Bird
Raised Fist0
Laravelframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Laravel command php artisan migrate do?
easy
A. It applies all pending migrations to update the database schema.
B. It deletes all tables from the database.
C. It rolls back the last batch of migrations.
D. It creates a new migration file.

Solution

  1. Step 1: Understand the purpose of migrations

    Migrations are used to update the database structure safely by applying changes.
  2. Step 2: Identify the command effect

    The command php artisan migrate applies all new migrations that have not been run yet.
  3. Final Answer:

    It applies all pending migrations to update the database schema. -> Option A
  4. Quick Check:

    Running migrations = apply changes [OK]
Hint: Migrate means apply new changes to the database [OK]
Common Mistakes:
  • Confusing migrate with rollback
  • Thinking migrate deletes tables
  • Assuming migrate creates files
2. Which of the following is the correct command to roll back the last batch of migrations in Laravel?
easy
A. php artisan migrate:run
B. php artisan migrate:reset
C. php artisan migrate:refresh
D. php artisan migrate:rollback

Solution

  1. Step 1: Identify rollback command

    The command to undo the last batch of migrations is php artisan migrate:rollback.
  2. Step 2: Differentiate from other commands

    migrate:reset rolls back all migrations, migrate:refresh rolls back and re-runs all, and migrate:run is invalid.
  3. Final Answer:

    php artisan migrate:rollback -> Option D
  4. Quick Check:

    Rollback = undo last batch [OK]
Hint: Rollback undoes last batch, reset undoes all [OK]
Common Mistakes:
  • Using migrate:reset to rollback last batch
  • Confusing migrate:refresh with rollback
  • Typing migrate:run which is invalid
3. Given these commands run in order:
php artisan migrate
php artisan migrate:rollback
What is the state of the database migrations after these commands?
medium
A. All migrations are applied.
B. No migrations are applied.
C. Only the last batch of migrations is undone.
D. Migrations are partially applied and partially undone randomly.

Solution

  1. Step 1: Apply migrations with migrate

    The first command applies all pending migrations, updating the database.
  2. Step 2: Rollback last batch

    The second command undoes only the last batch of migrations applied, not all.
  3. Final Answer:

    Only the last batch of migrations is undone. -> Option C
  4. Quick Check:

    Rollback undoes last batch only [OK]
Hint: Rollback only undoes last batch, not all [OK]
Common Mistakes:
  • Thinking rollback removes all migrations
  • Assuming rollback does nothing
  • Believing migrations are randomly undone
4. You run php artisan migrate:rollback but get an error saying "No migrations to rollback." What is the most likely cause?
medium
A. There are no migrations applied yet to rollback.
B. The migration files are missing from the migrations folder.
C. You need to run php artisan migrate:reset first.
D. The database connection is not configured.

Solution

  1. Step 1: Understand rollback error

    The error means Laravel found no migrations recorded as applied to undo.
  2. Step 2: Check migration status

    If no migrations were run before, rollback cannot undo anything, causing this error.
  3. Final Answer:

    There are no migrations applied yet to rollback. -> Option A
  4. Quick Check:

    No applied migrations = rollback error [OK]
Hint: Rollback needs applied migrations to undo [OK]
Common Mistakes:
  • Assuming rollback works without applied migrations
  • Thinking reset is needed before rollback
  • Ignoring database connection issues
5. You want to undo all migrations and then re-run them fresh. Which command should you use?
hard
A. php artisan migrate:rollback
B. php artisan migrate:refresh
C. php artisan migrate:reset
D. php artisan migrate:fresh

Solution

  1. Step 1: Understand the commands

    migrate:rollback undoes last batch, migrate:reset undoes all, migrate:refresh undoes all then re-runs, migrate:fresh drops all tables and migrates.
  2. Step 2: Choose command for undo and re-run

    migrate:refresh is the command that rolls back all migrations and then runs them again fresh.
  3. Final Answer:

    php artisan migrate:refresh -> Option B
  4. Quick Check:

    Refresh = rollback all + migrate again [OK]
Hint: Refresh rolls back all then migrates again [OK]
Common Mistakes:
  • Using rollback to undo all migrations
  • Confusing reset with refresh
  • Using fresh which drops tables instead of rollback
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard