Laravel 12 continues the tradition of using migrations to manage your database schema with version control. Whether you're creating tables, updating columns, or rolling back changes, migrations are your best friend.
This step-by-step guide will help you understand what migrations are, how they work, and how to use them effectively in Laravel 12.
📌 What Are Migrations in Laravel?
Laravel migrations are like version control for your database. They allow you to:
- Create or modify tables using PHP instead of SQL
- Share database changes across teams
- Roll back or re-run changes easily
- Seed test data alongside structure
Migrations live in the database/migrations
directory and are written in plain PHP with Laravel’s Schema Builder.
🛠️ Step 1: Create a New Migration
Use the Artisan command to create a migration:
This will create a new file like:
🧱 Step 2: Define the Table Schema
Open the file and define your table structure using the Schema
facade:
▶️ Step 3: Run the Migration
To apply your migrations and create the table in your database, run:
You’ll see:
✅ Your posts
table is now created in the database.
🔄 Step 4: Rolling Back Migrations
Need to undo a migration? Use:
To roll back only the last batch.
Or, roll back everything:
To drop all tables and re-run:
To re-run with seeders:
🧰 Step 5: Modify an Existing Table
To add new columns or indexes, create a migration using:
Then update it like this:
🔐 Step 6: Common Column Types in Laravel
Column Type | Usage Example |
---|---|
$table->string() |
Text up to 255 characters |
$table->text() |
Long text |
$table->boolean() |
True/False values |
$table->integer() |
Integer number |
$table->date() |
Date value |
$table->timestamps() |
Adds created_at and updated_at |
🚦 Step 7: Database Connection
Migrations run on the database configured in .env
:
Want to run a migration on a different connection?
📁 Laravel Migration File Structure
Every migration file name includes:
- A timestamp (for order)
- A descriptive name (
create_users_table
,add_status_to_posts_table
) - A
up()
method (applies changes) - A
down()
method (rolls back)
🧠 Best Practices
-
✅ Always add a rollback (
down()
) method - ✅ Keep migration files small and focused
- ✅ Use seeding (
php artisan db:seed
) for sample data - ✅ Never manually edit production database — use migrations!
✅ Summary
Task | Command/Function |
---|---|
Create migration | php artisan make:migration |
Run migrations | php artisan migrate |
Roll back last batch | php artisan migrate:rollback |
Re-run all migrations | php artisan migrate:refresh |
Add new column | Schema::table() |
Drop table or column | Schema::drop() or dropColumn() |
View migration status | php artisan migrate:status |