我想要的

仅回退:

回滚:2015_05_15_195423_alter_table_web_directories


我跑

PHP artisan migrate:rollback,我的3个迁移都在回滚。

Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_13_135240_create_web_directories_table
Rolled back: 2015_05_13_134411_create_contacts_table

我删除

我的web_directory和我的联系人表都是无意的。我不希望发生这种情况,如果我只能回滚特定的一个,这种灾难就永远不会发生。


当前回答

如果你不能做到@Martin Bean告诉你的,那么你可以尝试另一个技巧。

创建一个新的迁移,在该文件上,在up()方法中插入你想要回滚的迁移的down()方法中的内容,在down()方法中插入up()方法中的内容。

例如,如果你最初的迁移是这样的

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}
public function down()
{
    Schema::drop('users');
}

然后在新的迁移文件中这样做

public function up()
{
    Schema::drop('users');
}
public function down()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}

然后运行migrate,它会删除这个表。 如果你想要回滚它。

其他回答

拉拉维尔 5.3+

回退一步。本机。

php artisan migrate:rollback --step=1

这是手册页:文档。


Laravel 5.2及之前版本

不麻烦就没办法。详情请看Martin Bean的回答。

逐个迁移表。

将需要回退的迁移批号修改为最高。

运行迁移:回滚。

这可能不是处理大型项目的最佳方式。

现在回答这个问题可能有点晚了,但我觉得这里有一个非常好的、干净和有效的方法。我会尽量详细的。

在创建迁移之前,创建不同的目录,如下所示:

    database
       | 
       migrations
            |
            batch_1
            batch_2
            batch_3

然后,在创建迁移时运行以下命令(以表为例):

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3

上面的命令将迁移文件置于给定的目录路径中。然后,您可以简单地运行以下命令通过指定的目录迁移文件。

    php artisan migrate alter_table_web_directories --path=database/migrations/batch_1

*注意:您可以将batch_1更改为batch_2或batch_3或任何您存储迁移文件的文件夹名称。只要它仍然在database/migrations目录或某个指定的目录中。

接下来,如果你需要回滚特定的迁移,你可以批量回滚,如下所示:

    php artisan migrate:rollback --step=1
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1

or

    php artisan migrate:rollback --step=2
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2

    php artisan migrate:rollback --step=3
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3

使用这些技术将使您能够更灵活地控制数据库以及对模式的任何修改。

只回滚特定的迁移非常容易。 由于命令php artisan migrate:rollback,撤销上次数据库迁移, 迁移执行的顺序存储在迁移表中的批处理字段中。

您可以编辑要回滚的迁移的批处理值,并将其设置为较高的值。 然后你可以用一个简单的回滚迁移:

php artisan migrate:rollback

在编辑相同的迁移之后,可以使用简单的

php artisan migrate

注意:如果两个或多个迁移具有相同的更高值,它们将同时回滚。

回退一步。本机。

php artisan migrate:rollback --step=1

回滚两步。本机。

php artisan migrate:rollback --step=2