How to Rollback Migration in Laravel: Simple Guide
To rollback a migration in Laravel, use the
php artisan migrate:rollback command which undoes the last batch of migrations. You can also rollback multiple batches by adding the --step option with the number of batches to revert.Syntax
The basic syntax to rollback migrations in Laravel is:
php artisan migrate:rollback- Rolls back the last batch of migrations.php artisan migrate:rollback --step=2- Rolls back the last two batches.
The --step option controls how many batches to rollback.
bash
php artisan migrate:rollback [--step=n]
Example
This example shows how to rollback the last migration batch in Laravel.
bash
php artisan migrate:rollback
Output
Rolled back: 2024_06_01_000000_create_users_table
Rolled back: 2024_06_01_000001_create_posts_table
Common Pitfalls
Common mistakes when rolling back migrations include:
- Trying to rollback when no migrations have been run, which results in no action.
- Not specifying
--stepwhen you want to rollback multiple batches, causing only the last batch to rollback. - Confusing
migrate:rollbackwithmigrate:resetwhich rolls back all migrations.
bash
php artisan migrate:rollback --step=3 # Rolls back last 3 batches # Wrong: expecting all migrations to rollback php artisan migrate:rollback # Only last batch rolls back # To rollback all migrations use: php artisan migrate:reset
Quick Reference
| Command | Description |
|---|---|
| php artisan migrate:rollback | Rollback the last batch of migrations |
| php artisan migrate:rollback --step=2 | Rollback the last two batches |
| php artisan migrate:reset | Rollback all migrations |
| php artisan migrate:refresh | Rollback all migrations and re-run them |
Key Takeaways
Use
php artisan migrate:rollback to undo the last batch of migrations.Add
--step=n to rollback multiple batches at once.migrate:rollback only affects the last batch, not all migrations.Use
migrate:reset to rollback all migrations if needed.Always check migration status with
php artisan migrate:status before rolling back.