0
0
Laravelframework~10 mins

Running and rolling back migrations in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Running and rolling back migrations
Start Migration Command
Check Migration Files
Apply Migration Changes to DB
Update Migration Table
Migration Complete
Start Rollback Command
Check Last Batch Migrations
Revert Migration Changes in DB
Update Migration Table
Rollback Complete
This flow shows how Laravel applies migrations to update the database and how it rolls back the last batch of migrations to undo changes.
Execution Sample
Laravel
php artisan migrate
php artisan migrate:rollback
Run all pending migrations to update the database, then roll back the last batch of migrations to undo those changes.
Execution Table
StepCommandActionDatabase ChangeMigration Table Update
1php artisan migrateCheck for pending migrationsNo change yetNo change
2php artisan migrateApply migration: create users tableUsers table createdRecord migration batch 1
3php artisan migrateApply migration: add email columnEmail column addedRecord migration batch 1
4php artisan migrateMigration completeDatabase updatedMigration table updated with batch 1
5php artisan migrate:rollbackIdentify last batch migrationsNo change yetNo change
6php artisan migrate:rollbackRollback migration: remove email columnEmail column removedRemove migration record from batch 1
7php artisan migrate:rollbackRollback migration: drop users tableUsers table droppedRemove migration record from batch 1
8php artisan migrate:rollbackRollback completeDatabase revertedMigration table updated
9-No more migrations to rollbackNo changeNo change
💡 Rollback stops after last batch is undone; no more migrations to rollback.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 7Final
Database SchemaEmptyUsers table createdUsers table with email columnUsers table createdEmptyEmpty
Migration TableEmptyBatch 1 recorded (users table)Batch 1 recorded (users table + email column)Batch 1 recorded (users table + email column)EmptyEmpty
Key Moments - 2 Insights
Why does rollback only undo the last batch of migrations?
Because Laravel groups migrations into batches and rollback reverses only the most recent batch, as shown in execution_table steps 5-8.
What happens if you run migrate when no migrations are pending?
No changes occur; the command checks and finds no new migrations, so the database and migration table stay the same (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what database change happens at step 3?
AEmail column is added
BUsers table is created
CUsers table is dropped
DNo change
💡 Hint
Check the 'Database Change' column at step 3 in the execution_table.
At which step does the rollback remove the users table?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Look at the 'Action' and 'Database Change' columns for rollback steps in the execution_table.
If you run 'php artisan migrate' again after rollback, what happens?
ANothing happens, no migrations pending
BRollback runs automatically
CMigrations are applied again
DDatabase is reset
💡 Hint
Consider the variable_tracker showing database state after rollback and what migrate does with pending migrations.
Concept Snapshot
Laravel migrations update your database step-by-step.
Run 'php artisan migrate' to apply new migrations.
Run 'php artisan migrate:rollback' to undo the last batch.
Migrations are tracked in a special table.
Rollback only affects the most recent batch.
Always check migration status before running commands.
Full Transcript
This visual guide shows how Laravel runs and rolls back migrations. When you run 'php artisan migrate', Laravel looks for new migration files and applies them to update your database, like creating tables or adding columns. It records these changes in a migration table with batch numbers. When you run 'php artisan migrate:rollback', Laravel finds the last batch of migrations and reverses them in reverse order, updating the migration table accordingly. This process helps you manage database changes safely and step-by-step.