我想要的

仅回退:

回滚: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和我的联系人表都是无意的。我不希望发生这种情况,如果我只能回滚特定的一个,这种灾难就永远不会发生。


当前回答

php artisan migrate:rollback --path=/database/migrations/0000_00_00_0000_create_something_table.php

其他回答

如果你不能做到@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,它会删除这个表。 如果你想要回滚它。

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

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

    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——step=1"将迁移回退一步。

欲了解更多信息,请查看链接:https://laravel.com/docs/master/migrations#running-migrations

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

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

php artisan migrate:rollback

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

php artisan migrate

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

如果您查看您的迁移表,那么您将看到每个迁移都有一个批编号。因此,当您回滚时,它将回滚上一批中的每个迁移。

如果您只想回滚最后一次迁移,那么只需将批批号增加1。然后,下次运行rollback命令时,它将只回滚一次迁移,因为它是在自己的“批处理”中。

或者,从Laravel 5.3开始,你可以运行:

php artisan migrate:rollback --step=1

这将回滚最后一次迁移,不管它的批号是什么。