Migrations help you change your database step-by-step. Running migrations applies changes. Rolling back undoes them if needed.
Running and rolling back migrations in Laravel
php artisan migrate php artisan migrate:rollback
php artisan migrate runs all new migrations.
php artisan migrate:rollback undoes the last batch of migrations.
php artisan migrate
php artisan migrate:rollback
php artisan migrate:rollback --step=2php artisan migrate:fresh
This migration creates a books table with id, title, author, and timestamp columns. Running php artisan migrate applies this change. Running php artisan migrate:rollback removes the books table.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateBooksTable extends Migration { public function up() { Schema::create('books', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('author'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('books'); } } // To run this migration, use: // php artisan migrate // To roll back this migration, use: // php artisan migrate:rollback
Always back up your data before rolling back migrations in production.
Rolling back only affects the last batch of migrations run together.
You can use --step to roll back multiple migrations at once.
Running migrations applies new database changes.
Rolling back migrations undoes recent changes.
Use these commands to manage your database structure safely.
Practice
php artisan migrate do?Solution
Step 1: Understand the purpose of migrations
Migrations are used to update the database structure safely by applying changes.Step 2: Identify the command effect
The commandphp artisan migrateapplies all new migrations that have not been run yet.Final Answer:
It applies all pending migrations to update the database schema. -> Option AQuick Check:
Running migrations = apply changes [OK]
- Confusing migrate with rollback
- Thinking migrate deletes tables
- Assuming migrate creates files
Solution
Step 1: Identify rollback command
The command to undo the last batch of migrations isphp artisan migrate:rollback.Step 2: Differentiate from other commands
migrate:resetrolls back all migrations,migrate:refreshrolls back and re-runs all, andmigrate:runis invalid.Final Answer:
php artisan migrate:rollback -> Option DQuick Check:
Rollback = undo last batch [OK]
- Using migrate:reset to rollback last batch
- Confusing migrate:refresh with rollback
- Typing migrate:run which is invalid
php artisan migratephp artisan migrate:rollbackWhat is the state of the database migrations after these commands?
Solution
Step 1: Apply migrations with migrate
The first command applies all pending migrations, updating the database.Step 2: Rollback last batch
The second command undoes only the last batch of migrations applied, not all.Final Answer:
Only the last batch of migrations is undone. -> Option CQuick Check:
Rollback undoes last batch only [OK]
- Thinking rollback removes all migrations
- Assuming rollback does nothing
- Believing migrations are randomly undone
php artisan migrate:rollback but get an error saying "No migrations to rollback." What is the most likely cause?Solution
Step 1: Understand rollback error
The error means Laravel found no migrations recorded as applied to undo.Step 2: Check migration status
If no migrations were run before, rollback cannot undo anything, causing this error.Final Answer:
There are no migrations applied yet to rollback. -> Option AQuick Check:
No applied migrations = rollback error [OK]
- Assuming rollback works without applied migrations
- Thinking reset is needed before rollback
- Ignoring database connection issues
Solution
Step 1: Understand the commands
migrate:rollbackundoes last batch,migrate:resetundoes all,migrate:refreshundoes all then re-runs,migrate:freshdrops all tables and migrates.Step 2: Choose command for undo and re-run
migrate:refreshis the command that rolls back all migrations and then runs them again fresh.Final Answer:
php artisan migrate:refresh -> Option BQuick Check:
Refresh = rollback all + migrate again [OK]
- Using rollback to undo all migrations
- Confusing reset with refresh
- Using fresh which drops tables instead of rollback
Ready to prove it?
Pass the test and this pattern moves unknown
