gpt4 book ai didi

php - 如何修改 Laravel 中的迁移?

转载 作者:IT王子 更新时间:2023-10-29 00:10:14 24 4
gpt4 key购买 nike

我正在尝试修改现有迁移。这是我当前的迁移类:

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

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

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

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

Nothing to migrate.

如何应用新版本的迁移?

最佳答案

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

php artisan make:migration update_error_message_in_log_for_user_table

然后,在创建的迁移类中,使用 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();
});
}
}

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

php artisan migrate

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

php artisan migrate:rollback

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

php artisan migrate:rollback --step=5

See more about Modifying columns with Migration

关于php - 如何修改 Laravel 中的迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41205844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com