Bird
Raised Fist0
Laravelframework~20 mins

Running and rolling back migrations in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you run php artisan migrate in Laravel?
You run php artisan migrate in a Laravel project with new migration files. What is the result?
ALaravel applies all pending migrations and updates the database schema accordingly.
BLaravel rolls back the last batch of migrations automatically.
CLaravel only creates migration files but does not change the database.
DLaravel deletes all existing tables and recreates them from scratch.
Attempts:
2 left
💡 Hint
Think about what 'migrate' means in database terms.
❓ state_output
intermediate
2:00remaining
What is the effect of php artisan migrate:rollback?
After running several migrations, you execute php artisan migrate:rollback. What happens to the database?
AThe database is backed up but no schema changes occur.
BAll migrations are rolled back, clearing the entire database schema.
CNo changes happen; the command only shows migration status.
DThe last batch of migrations is reversed, undoing their database changes.
Attempts:
2 left
💡 Hint
Rollback means undo the last step.
📝 Syntax
advanced
2:00remaining
Which command rolls back the last 3 batches of migrations?
You want to undo the last 3 batches of migrations in Laravel. Which command is correct?
Aphp artisan migrate:rollback --step=3
Bphp artisan migrate:rollback -n 3
Cphp artisan migrate:rollback --batches=3
Dphp artisan migrate:rollback 3
Attempts:
2 left
💡 Hint
Check the official Laravel syntax for rollback steps.
🔧 Debug
advanced
2:00remaining
Why does php artisan migrate:rollback fail with 'No migrations to rollback'?
You run php artisan migrate:rollback but get the error 'No migrations to rollback'. What is the most likely reason?
AThe migration files have syntax errors preventing rollback.
BThere are no migrations applied yet to rollback.
CThe database connection is missing in the config file.
DThe rollback command requires a --force flag to run.
Attempts:
2 left
💡 Hint
Think about what rollback needs to undo.
🧠 Conceptual
expert
3:00remaining
What is the difference between php artisan migrate:refresh and php artisan migrate:rollback?
Choose the option that best explains the difference between these two Laravel migration commands.
A<code>migrate:refresh</code> only rolls back the last batch; <code>migrate:rollback</code> rolls back all migrations.
B<code>migrate:refresh</code> creates new migration files; <code>migrate:rollback</code> deletes migration files.
C<code>migrate:refresh</code> rolls back all migrations and then runs them again; <code>migrate:rollback</code> only rolls back the last batch.
D<code>migrate:refresh</code> deletes the database; <code>migrate:rollback</code> backs up the database.
Attempts:
2 left
💡 Hint
Refresh means start fresh by undoing all and reapplying.

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