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

问题描述 投票:2回答:3

我已经使用了 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');
    });

并增加了三个字段,并给予 php artisan migrate 并将这些字段存储到数据库中

还发现 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 再次...

php laravel laravel-4 migration database-migration
3个回答
11
投票

你应该有一个 down() 方法,应该是这样的。

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

这个方法会在回滚时被调用 并负责删除迁移中添加的列。


0
投票

根据laravel 7+文档, 当你需要同时删除多个列时, 这个方法也可以使用.

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

-1
投票

添加 下来 公用函数滴 用户 回滚时的表格...

public function down()
{
    Schema::drop('users');
}
© www.soinside.com 2019 - 2024. All rights reserved.