Rollback one specific migration in Laravel

前端 未结 19 1480
梦毁少年i
梦毁少年i 2020-12-02 04:14

I want

to rollback only :

Rolled back: 2015_05_15_195423_alter_table_web_directories


I run

php artisan migrate:roll

相关标签:
19条回答
  • 2020-12-02 05:01

    If you can't do what is told by @Martin Bean, then you can try another trick.

    Create a new migration and on that file in up() method insert what's in down() method of the migration you want to rollback and in down() method insert what's in up() method.

    e.g if your original migration is like this

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

    then in new migration file do this

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

    and then run the migrate, it will delete the table. and if you again want that back just rollback it.

    0 讨论(0)
  • 2020-12-02 05:02

    If you want to rollback last migration.

    php artisan migrate:rollback
    

    If you want to rollback specific migration then go to migration table and set highest value of that record in batch. Then.

    php artisan migrate:rollback
    

    Currently i'm working on laravel 5.8 if not working any other version of laravel please inform to me.

    0 讨论(0)
  • 2020-12-02 05:02

    Migrate tables one by one.

    Change the batch number of the migration you want to rollback to the highest.

    Run migrate:rollback.

    May not be the most comfortable way to deal with larger projects.

    0 讨论(0)
  • 2020-12-02 05:02
    INSERT INTO homestead.bb_migrations (`migration`, `batch`)  VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2')
    

    something like this

    0 讨论(0)
  • 2020-12-02 05:03

    As stated in the Laravel manual, you may roll back specific number of migrations using the --step option

    php artisan migrate:rollback --step=5
    
    0 讨论(0)
  • 2020-12-02 05:06

    If you have access to the DB you have a easier solution. You can delete the record from migrations table and then just drop the table. with SQL client.

    And Can use

    php artisan migrate:rollback --path=... 
    

    Like many answers. And remember the path is Location. You can remove even module migration like this. (Any migration from anywhare)

    php artisan migrate:rollback --path=Modules/YourModule/database/migrations/2020_05_15_xxxxxx_create_your_table.php
    

    And remember, If you are using linux servers careful about case sensitivity. You have to add like /Database/Migrations with starting capital.

    /Database/Migrations/2020_05_15_xxxxxx_create_your_table.php
    
    0 讨论(0)
提交回复
热议问题