如何在Laravel中修改迁移?

sta*_*ack 17 php migration laravel

我正在尝试修改现有的迁移.我的意思是,这是我目前的迁移类:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

我也曾执行php artisan migrate过一次这个命令.现在我需要向列中添加->nullable()方法error_message.所以我编辑了我的迁移,如下所示:

.
.
    $table->string('error_message')->nullable();
.
.
Run Code Online (Sandbox Code Playgroud)

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

什么都没有迁移.

无论如何,我如何应用新版本的迁移?

Sau*_*ogi 22

您应该使用命令创建新的迁移:

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

然后,在该创建的迁移类中,使用如下change方法添加此行:

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();
        });
    }
}
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)

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

php artisan migrate:rollback --step=5
Run Code Online (Sandbox Code Playgroud)

详细了解如何使用迁移修改列


Ale*_*nin 7

如果您的应用程序尚未投入生产,并且您为数据添加了种子,那么您最好的方法是运行:

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

此命令将删除所有表并重新创建它们。然后它将播种数据。

如果要在开发过程中为每个更改创建其他迁移,那么最终将获得数百个迁移类。