Say i got a.php
, b.php
, c.php
and d.php
migration classes files. How to rollback to a specific migration state, the state defined within b.php
for example, with artisan command ?

- 86,191
- 22
- 213
- 204

- 621
- 1
- 7
- 18
-
Can you just run "php artisan migrate:rollback" twice - or do you want to *specifically* go to b.php? – Laurence Jul 17 '13 at 10:28
-
I've thought about running "php artisan migrate:rollback" several times. But what if I have a.php, b.php and c.php at the time and run through them altogether and then create&migrate d.php. The b.php would never be rolled back for this situation so I want something to make it specifically rolling back to b.php. – do. Jul 18 '13 at 03:55
9 Answers
I am afraid, you cannot do this directly.
You can: 1, Rollback The Last Migration Operation (all migrations ran in the last batch)
php artisan migrate:rollback
2, Rollback all migrations
php artisan migrate:reset
3, Rollback all migrations and run them all again
php artisan migrate:refresh
php artisan migrate:refresh --seed
In your situation, modify b.php and it's up()
method, then execute artisan migrate:refresh
command.

- 37,464
- 17
- 118
- 167

- 22,476
- 5
- 61
- 65
-
Caution: `php artisan migrate:rollback` apparently will now rollback all your migrations. – Vigs Sep 07 '15 at 20:00
-
3To revises my comment above - if you have a large migration of many tables etc, and you run `migrate:rollback` it will rollback all of the actions from the last migration, not just the last action. I incorrectly assumed it would only rollback the last action, not the whole migration. – Vigs Sep 07 '15 at 22:13
-
3As of 5.3 (released after this answer was posted), you can set the number of steps you would like to rollback, e.g. php artisan migrate:rollback --step=5 as you can see listed here: https://laravel.com/docs/5.3/migrations#rolling-back-migrations . @Yauheni Prakopchyk mentions it below. – Marty Nov 16 '16 at 16:29
There's a way to hack it by manually editing the database. In the migrations
table change the batch
column by giving the last migration a different batch number. Be aware that they are in increasing order, so edit accordingly. This tracks which migrations were applied separately.
Then run artisan:rollback
and it will undo the last "batch".
So if you want to separate them all, then start from the top and give each 1,2,3,4,5 and so on... You can see that it is easily scriptable, and you can make an artisan command if you wish to separate all your migrations.

- 16,205
- 16
- 71
- 126

- 453
- 4
- 10
-
3
-
How can I edit my batch column ? It seems lock and grey-out ? Can anyone please kindly advise ? – code-8 May 17 '15 at 15:52
-
1
In my experience. I never do migrate:rollback. I would usually create another migration that does all the changes i need to "undo/rollback" the previous migrations.
This way you can be flexible if you want to rollback 2-x steps back, you can just create a new migration to effect the changes you want and then run the new migration by php artisan migrate.

- 51
- 1
- 1
Laravel 5.3
With Laravel 5.3 there is no need for heavy scripting. As it allows to rollback given number of migrations.
php artisan migrate:rollback --step=1
Here's the manual page for reference.

- 10,202
- 4
- 33
- 37
-
This is the best answer, as it's actually current. Most other answers are outdated. – GTS Joe Sep 27 '20 at 00:40
In fact, there is not this feature (yet). surprisingly
The best idea, is create a new file backtob.php and make its up call the down of your other migrate files. To avoid copy and paste, you can do something like this:
class BacktoB {
public function up () {
// the database is in the after D state //
$migrateD = new D();
$migrateD->down();
// the database is in the after C state //
$migrateC = new C();
$migrateC->down();
// the database is in the before C state //
// before C = B //
}
public function down () {
// the database is in the B state //
$migrateC = new C();
$migrateC->up();
// the database is in the after C state //
$migrateD = new D();
$migrateD->up();
// the database is in the after D state //
}
}
As you can see, you can create the up and down calling the up and down of those migrations what you want to revert.
It is not the ideal, but it is what we can do.

- 2,825
- 33
- 32
If you really wanted to - you could write a custom function that queries the migrations table, looks for the file you are after, and works out how many times to roll back - then does a loop of 'migrate:rollback' until you reach the required migration...

- 58,936
- 21
- 171
- 212
There is an easy yet dirty way:
If you have migrations a.php
, b.php
and c.php
and want to rollback c
and b
, you can simply modify a.php
in such a way that there will be a syntax error... drop a semi-colon or something.
So, when you run php artisan migrate:rollback
it will rollback both c
and b
and stop with an error in a
. From then on, the rollback of c
and b
will be considered the last migration operation.
Don't forget to fix whatever error you made on purpose in a.php
.

- 191
- 8
Since Laravel just provide php artisan migrate:rollback to rollback your migration script, the best way to rollback your selected migration script is to create a new migration script and put the script in your down method (on your selected migration script) to the newly created migration script. Hope this help.

- 163
- 2
- 12
Use the php artisan migrate:rollback command.
php artisan migrate:rollback
To see what rollback will do, use the --pretend option.
php artisan migrate:rollback --pretend
You can also specify a database connection other than the default one.
php artisan migrate:rollback --pretend --database=other-one

- 804
- 8
- 9