In this article we will learn rollback specific migration in laravel, in our recent article i wrote about creating migration in laravel and sometimes after creating migration we want to rollback specific migration laravel so in this article i will show you to rollback a specific migration in laravel. Laravel artisan have multiple options to rollback the migration like we can rollback all migrations, we can rollback last n number of migration using step option and in the same way we can rollback all specific rollback using reset option.
Rollback specific migration in laravel
In relational databases rollback is a technique to revert back the database operation in previous state. In the same way laravel maintains it migrations in file and database, and we can rollback the operation to its previous state using artisan command.
However there is no direct command to rollback the specific migration but we can use this blow tricks to rollback the migration.
Steps to rollback specific migration
Step 1 : Open database and check migrations table
In this step we will open our database using any tool like phpMyAdmin
or command line then check migrations table.
As you can see we have migration table and i want to rollback 2022_02_15_174050_add_status_movies_table
Step 2 : Change batch number to last number
Now, change the batch number of 2022_02_15_174050_add_status_movies_table
to last number of batch number , In our example last number is 8
so change the 2022_02_15_174050_add_status_movies_table
batch to 9
9 | 2022_02_15_174050_add_status_movies_table | 9 |
Step 3 : Run artisan rollback command
Run the artisan command rollback to make the changes
php artisan migrate:rollback --step=1
So here we are rollback the last batch which is 9
Rolling back: 2022_02_15_174050_add_status_movies_table
Rollback last migrations using step option
We can also rollback last migration using the step option in artisan command, step option accept numeric parameter to rollback last migrations. For example if --step =2
then it means it will rollback last two migrations.
here is the command
php artisan migrate:rollback --step=3
Rollback last batch migrations
We can also rollback last batch of migration using the migration rollback command without any options as below
Here is the command
php artisan migrate:rollback
Rollback all migrations and reset database
We can also rollback all migration and also can reset the database migration to start point using artisan migration:reset
command
Here is the command
php artisan migrate:reset
Rollback all and Re run all migrations
refresh
option is used to rollback all migration and it runs again migration command to re create the database
Here is the command
php artisan migrate:refresh