How can I modify a migration in Laravel?(如何在 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
这篇关于如何在 Laravel 中修改迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Laravel 中修改迁移?


- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Laravel 仓库 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01