🧱 Understanding Laravel 12 Migrations

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:

php artisan make:migration create_posts_table

This will create a new file like:

database/migrations/2025_07_18_000000_create_posts_table.php

🧱 Step 2: Define the Table Schema

Open the file and define your table structure using the Schema facade:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); // created_at and updated_at }); } public function down(): void { Schema::dropIfExists('posts'); } };

▶️ Step 3: Run the Migration

To apply your migrations and create the table in your database, run:

php artisan migrate

You’ll see:

Migrating: 2025_07_18_000000_create_posts_table Migrated: 2025_07_18_000000_create_posts_table

✅ Your posts table is now created in the database.


🔄 Step 4: Rolling Back Migrations

Need to undo a migration? Use:

php artisan migrate:rollback

To roll back only the last batch.

Or, roll back everything:

php artisan migrate:reset

To drop all tables and re-run:

php artisan migrate:refresh

To re-run with seeders:

php artisan migrate:refresh --seed

🧰 Step 5: Modify an Existing Table

To add new columns or indexes, create a migration using:

php artisan make:migration add_status_to_posts_table

Then update it like this:

public function up(): void { Schema::table('posts', function (Blueprint $table) { $table->string('status')->default('draft'); }); } public function down(): void { Schema::table('posts', function (Blueprint $table) { $table->dropColumn('status'); }); }

🔐 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:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db DB_USERNAME=root DB_PASSWORD=

Want to run a migration on a different connection?

php artisan migrate --database=sqlite

📁 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

Post a Comment

Previous Post Next Post

Contact Form