在迁移中向现有表添加新列

kim*_*sen 229 php laravel laravel-4 laravel-migrations

我无法弄清楚如何使用Laravel框架将新列添加到现有数据库表中.

我尝试使用...编辑迁移文件

<?php

public function up()
{
    Schema::create('users', function ($table) {
        $table->integer("paid");
    });
}
Run Code Online (Sandbox Code Playgroud)

在终端,我执行php artisan migrate:installmigrate.

如何添加新列?

Phi*_*rks 525

要创建迁移,可以在Artisan CLI上使用migrate:make命令.使用特定名称可避免与现有模型发生冲突

对于Laravel 3:

php artisan migrate:make add_paid_to_users
Run Code Online (Sandbox Code Playgroud)

对于Laravel 5+:

php artisan make:migration add_paid_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)

然后,您需要使用该Schema::table()方法(因为您正在访问现有表,而不是创建新表).你可以添加这样的列:

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}
Run Code Online (Sandbox Code Playgroud)

并且不要忘记添加回滚选项:

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('paid');
    });
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以运行迁移:

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

Laravel 3的文档中都详细介绍了这一点:

对于Laravel 4/Laravel 5:

编辑:

用于$table->integer('paid')->after('whichever_column');在特定列之后添加此字段.

  • 从Laravel 5开始,这个命令现在是`php artisan make:migration add_paid_to_users` (5认同)
  • 只是`php artisan migrate` (3认同)

cam*_*ase 61

我将使用Laravel 5.1及其后的版本为未来读者添加mike3875的答案.

为了使事情更快,您可以使用标志"--table",如下所示:

php artisan make:migration add_paid_to_users --table="users"
Run Code Online (Sandbox Code Playgroud)

这将自动添加updown方法内容:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        //
    });
}
Run Code Online (Sandbox Code Playgroud)

类似地,您可以--create["table_name"]在创建新迁移时使用该选项,这将为迁移添加更多样板.小点,但在加载它们时很有帮助!

  • Laravel 5.0 不是这种情况,Laravel 5.1 中添加了`Blueprint`。只需澄清一点即可。 (2认同)

mik*_*yuk 24

如果您使用的是Laravel 5,那么命令就是;

php artisan make:migration add_paid_to_users
Run Code Online (Sandbox Code Playgroud)

所有用于制作的命令(控制器,模型,迁移等)都已在make:命令下移动.

php artisan migrate 虽然仍然是一样的.


che*_*aby 18

laravel 5.6及以上

如果您想将新列添加为现有表的FOREIGN KEY.

通过执行以下命令创建新迁移:make:migration

示例:

php artisan make:migration add_store_id_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)

在database/migrations文件夹中,您有新的迁移文件,例如:

2018_08_08_093431_add_store_id_to_users_table.php(见评论)

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStoreIdToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            // You probably want to make the new column nullable
            $table->integer('store_id')->unsigned()->nullable()->after('password');

            // 2. Create foreign key constraints
            $table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Drop foreign key constraints
            $table->dropForeign(['store_id']);

            // 2. Drop the column
            $table->dropColumn('store_id');
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

之后运行命令:

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

如果您因任何原因要撤消上次迁移,请运行以下命令:

php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)

您可以在文档中找到有关迁移的更多信息

  • 非常全面和相关的答案。谢谢! (2认同)

tpl*_*ner 17

您可以在初始Schema::create方法中添加新列,如下所示:

Schema::create('users', function($table) {
    $table->integer("paied");
    $table->string("title");
    $table->text("description");
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

如果您已经创建了表,则可以通过创建新迁移并使用该Schema::table方法向该表添加其他列:

Schema::table('users', function($table) {
    $table->string("title");
    $table->text("description");
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

文档对此非常透彻,并且从版本3版本4没有太大变化.


Reh*_*had 9

Laravel 7

  1. 使用 cli 命令创建迁移文件:

    php artisan make:migration add_paid_to_users_table --table=users

  2. 将在迁移文件夹中创建一个文件,在编辑器中打开它。

  3. 添加到函数 up():

Schema::table('users', function (Blueprint $table) {
    // Create new column
    // You probably want to make the new column nullable
    $table->integer('paid')->nullable()->after('status');
}
Run Code Online (Sandbox Code Playgroud)
  1. 添加到函数 down(),这将在迁移因某些原因失败时运行:

    $table->dropColumn('paid');

  2. 使用 cli 命令运行迁移:

    php artisan migrate


如果您想向表中添加一列以创建外键约束:

在上述过程的第 3 步中,您将使用以下代码:

$table->bigInteger('address_id')->unsigned()->nullable()->after('tel_number');

$table->foreign('address_id')->references('id')->on('addresses')->onDelete('SET NULL');
Run Code Online (Sandbox Code Playgroud)

在上述过程的第 4 步中,您将使用以下代码:

// 1. Drop foreign key constraints
$table->dropForeign(['address_id']);
// 2. Drop the column
$table->dropColumn('address_id');
Run Code Online (Sandbox Code Playgroud)


小智 8

this things is worked on laravel 5.1.

first, on your terminal execute this code

php artisan make:migration add_paid_to_users --table=users
Run Code Online (Sandbox Code Playgroud)

after that go to your project directory and expand directory database - migration and edit file add_paid_to_users.php, add this code

public function up()
{
    Schema::table('users', function (Blueprint $table) {
         $table->string('paid'); //just add this line
    });
}
Run Code Online (Sandbox Code Playgroud)

after that go back to your terminal and execute this command

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

hope this help.


小智 8

步骤1

php artisan make:migration add_sex_to_users_table --table=users
Run Code Online (Sandbox Code Playgroud)

第2步

在新生成的迁移文件中,你会发现up和down的hook方法。在上钩中添加要添加的列,在下钩中添加需要删除的列。例如,我需要在用户列中添加性别,因此我将在向上挂钩中添加以下行。

$table->integer('quantity')->default(1)->nullable();
Run Code Online (Sandbox Code Playgroud)

所以我有这样的东西

public function up()
{
    Schema::table('service_subscriptions', function (Blueprint $table) {
        $table->integer('quantity')->default(1)->nullable();
    });
}
Run Code Online (Sandbox Code Playgroud)

步骤3

运行迁移命令如下

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

然后您将添加一个新列


小智 7

在 Laravel 8

php artisan make:migration add_columnname_to_tablename_table --table=tablename
Run Code Online (Sandbox Code Playgroud)

然后在创建迁移之后

public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            $table->datatype('column_name')->nullable();
        });
    }
public function down()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            $table->dropColumn('column_name');
        });
    }
Run Code Online (Sandbox Code Playgroud)

然后运行

php artisan migrate
Run Code Online (Sandbox Code Playgroud)

如果您遇到错误,请使用创建表之前的日期重命名迁移名称,然后再次运行 php artisan migrate


小智 6

您只需修改现有的迁移文件,例如在表中添加一列,然后在终端中键入:

$ php artisan migrate:refresh
Run Code Online (Sandbox Code Playgroud)

  • 刷新将清空表 (9认同)
  • 这是非常危险的 - 如果有人运行旧版本,有些人会有新版本,并且随之而来的是混乱.在Liquibase中,如果您编辑文件,它将失败,除非您明确放入允许它的例外,并且您只能在极少数情况下执行此操作.例如,如果在某些数据库中已有空数据时使列不为null,则它将中断. (8认同)
  • 最好是,如果您编辑答案并提到它将清空您的表,那会更好。 (3认同)

小智 6

首先回滚之前的迁移

php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)

之后,您可以修改现有的迁移文件(添加新的、重命名或删除列),然后重新运行您的迁移文件

php artisan migrate
Run Code Online (Sandbox Code Playgroud)