Migrations help you change your database step-by-step. Running migrations applies changes. Rolling back undoes them if needed.
0
0
Running and rolling back migrations in Laravel
Introduction
When you add a new table or column to your database
When you fix a mistake in a previous database change
When you want to reset your database to a previous state during development
When you share your database changes with your team
When testing database changes before going live
Syntax
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.
Examples
This command runs all migrations that have not been run yet.
Laravel
php artisan migrate
This command rolls back the last batch of migrations that were run.
Laravel
php artisan migrate:rollback
This rolls back the last two migrations.
Laravel
php artisan migrate:rollback --step=2This command deletes all tables and runs all migrations fresh.
Laravel
php artisan migrate:fresh
Sample Program
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.
Laravel
<?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
OutputSuccess
Important Notes
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.
Summary
Running migrations applies new database changes.
Rolling back migrations undoes recent changes.
Use these commands to manage your database structure safely.