如何在 Laravel 中修改迁移?

2022-01-23 migration php laravel

我正在尝试修改现有迁移.这是我当前的迁移课程:

类 CreateLogForUserTable 扩展了迁移{公共函数 up(){Schema::create('log_for_user', function (Blueprint $table) {$table->increments('id');$table->integer('user_id');$table->string('table_name');$table->string('error_message');$table->unsignedTinyInteger('error_code');$table->timestamps();});}公共函数向下(){架构::drop('log_for_user');}}

我已经执行了一次 php artisan migrate 命令.现在我需要将 ->nullable() 方法添加到 error_message 列.所以我编辑了我的迁移,如下所示:

<代码>..$table->string('error_message')->nullable();..

但是当我再次执行 php artisan migrate 时,它说:

<块引用>

没有要迁移的东西.

如何应用新版本的迁移?

解决方案

你应该使用命令创建一个新的迁移:

php artisan make:migration update_error_message_in_log_for_user_table

然后,在创建的迁移类中,使用 change 方法添加这一行,如下所示:

类 UpdateLogForUserTable 扩展了迁移{公共函数 up(){Schema::table('log_for_user', function (Blueprint $table) {$table->string('error_message')->nullable()->change();});}公共函数向下(){Schema::table('log_for_user', function (Blueprint $table) {$table->string('error_message')->change();});}}

要进行这些更改并运行迁移,请使用以下命令:

php artisan 迁移

要回滚更改,请使用以下命令:

php artisan migrate:rollback

您可以通过向回滚命令提供 step 选项来回滚有限数量的迁移.例如,以下命令将回滚最近五次迁移:

php artisan migrate:rollback --step=5

<块引用>

详细了解使用迁移修改列

I'm trying to modify a existing migration. Here is my current migration class:

class CreateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::create('log_for_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('table_name');
            $table->string('error_message');
            $table->unsignedTinyInteger('error_code');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('log_for_user');
    }
}

I've executed the php artisan migrate command once. Now I need to add ->nullable() method to the error_message column. So I edited my migration, something like this:

.
.
    $table->string('error_message')->nullable();
.
.

But when I execute php artisan migrate again, it says:

Nothing to migrate.

How can I apply the new version of the migration?

解决方案

You should create a new migration using command:

php artisan make:migration update_error_message_in_log_for_user_table

Then, in that created migration class, add this line, using the change method like this:

class UpdateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->change();
        });
    }
}

To make these changes and run the migration, use the command:

php artisan migrate

and to rollback the changes, use the command:

php artisan migrate:rollback

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:

php artisan migrate:rollback --step=5

See more about Modifying columns with Migration

相关文章