Rollback a specific migration file in Laravel
- How to Rollback Specific Migration Files in Laravel
- Understanding Laravel Migration Rollbacks
- Rollback a Specific Migration File
- Rollback Multiple Specific Migration Files
- Reset or Refresh Specific Migration Files
How to Rollback Specific Migration Files in Laravel
Rolling back migrations is a normal part of Laravel development. You might add a migration, test it, and then need to undo only that specific change without affecting others. Laravel makes this possible through the --path option, allowing you to target individual migration files precisely.
Understanding Laravel Migration Rollbacks
Laravel migrations are version-controlled database schema files. When you run php artisan migrate, each migration file is executed in order, and its name is recorded in the migrations table.
To undo migrations, Laravel provides the following key commands:
-
php artisan migrate:rollback -
php artisan migrate:reset -
php artisan migrate:refresh
Each can be combined with the --path option to rollback specific files instead of all migrations.
Rollback a Specific Migration File
If you want to rollback a single specific migration, use the migrate:rollback command with the --path option.
php artisan migrate:rollback --path=/database/migrations/2024_11_04_123456_create_orders_table.php
How It Works
- The
--pathoption tells Laravel which migration file to target. - The path should be relative to the base Laravel project directory.
- Laravel will execute the
down()method of that specific migration file only.
This is the most direct and correct way to rollback one migration without touching others.
Rollback Multiple Specific Migration Files
You can rollback multiple specific files by specifying each --path argument individually:
php artisan migrate:rollback \--path=/database/migrations/2024_11_04_123456_create_orders_table.php \--path=/database/migrations/2024_11_04_123457_add_status_to_orders_table.php
Each listed file will have its down() method executed in order, rolling back only those migrations.
Reset or Refresh Specific Migration Files
In addition to rolling back, Laravel also lets you reset or refresh specific migrations.
Reset Specific Migration
To completely reset (rollback all migrations), but limit the rollback to certain files:
php artisan migrate:reset --path=/database/migrations/2024_11_04_123456_create_orders_table.php
Refresh Specific Migration
To rollback and re-run the same migration (useful during testing):
php artisan migrate:refresh --path=/database/migrations/2024_11_04_123456_create_orders_table.php
This command will:
- Run the
down()method (rollback). - Immediately run the
up()method again (reapply the migration).
Stay Updated.
I'll you email you as soon as new, fresh content is published.
Latest Posts