Laravel-migrate:回滚添加和删除表列


Laravel migrate:rollback adding and deleting table columns

我使用了php artisan migrate:make add_something_to_to_user_table --table=users

和编码

Schema::table('users', function(Blueprint $table)
    {
        $table->string('description1');
        $table->string('description2');
        $table->string('description3');
    });

并添加了三个字段,给出了CCD_ 2,这些字段被存储到数据库中

还发现migration table用行2014_11_05_145536_add_something_to_to_user_table 更新

现在当我使用php artisan migrate:rollback

迁移表中的2014_11_05_145536_add_something_to_to_user_table行丢失,但添加到用户表的列保持相同

为什么不删除表中的字段这会导致再次使用php artisan migrate时出错。。。

您的迁移中应该有一个down()方法,它应该如下所示:

public function down()
{
    Schema::table('users', function($table)
    {
        $table->dropColumn(array('description1', 'description2', 'description3'));
    });
}

这将在回滚时调用,并负责删除迁移添加的列。

根据laravel 7+doc,当您需要同时删除多个列时,这也会起作用。

public function down(){
    Schema::table('users', function (Blueprint $table) {
     $table->dropColumn(['description1', 'description2', 'description3']);
   });
}

添加down公共函数以在回滚时删除用户表。。

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