Mastering Laravel 12 Migrations: Version-Control Your Database Schema
When building any web application, database schema management becomes a big deal. You add tables, update columns, remove fields… and before you know it, things get messy.
That’s where Laravel Migrations come in. They are like version control for your database, allowing you to define and track changes over time.
In this post, we'll dive deep into Laravel 12 migrations: how they work, how to use them, and best practices for clean and reliable database versioning.
🧠 What Are Migrations in Laravel?
“Migrations are like Git for your database.” – Laravel Docs
Laravel Migrations are PHP files that define the structure of your database tables using a clean, expressive syntax. You can use them to:
- Create tables
- Add or drop columns
- Rename tables
- Add indexes
- Roll back changes
This helps keep your schema in sync across all environments and developers.
🛠 How to Create a Migration
Let’s start by creating a migration file.
php artisan make:migration create_employees_table
You’ll find the new file in the database/migrations folder with a timestamp:
database/migrations/2025_05_18_000000_create_employees_table.php
🧱 Structure of a Migration File
Here’s a basic example:
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('employees', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('position');
$table->string('department');
$table->decimal('salary', 10, 2);
$table->date('hire_date');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('employees');
}
};
up()method: Defines what happens when the migration is run.down()method: Defines how to revert the migration.
▶️ Running Migrations
To apply all pending migrations:
php artisan migrate
You'll see output like:
Migrating: 2025_05_18_000000_create_employees_table
Migrated: 2025_05_18_000000_create_employees_table (123.45ms)
This creates the employees table in your database!
⏪ Rolling Back Migrations
Mistakes happen. Laravel has your back!
Rollback the last batch:
php artisan migrate:rollback
Rollback all migrations:
php artisan migrate:reset
Refresh all (rollback + re-run):
php artisan migrate:refresh
Pro tip: Add --seed to also re-run seeders:
php artisan migrate:refresh --seed
✨ Updating Tables with New Migrations
Need to add a column later? No problem.
php artisan make:migration add_email_to_employees_table
Laravel will generate:
public function up(): void
{
Schema::table('employees', function (Blueprint $table) {
$table->string('email')->after('name');
});
}
public function down(): void
{
Schema::table('employees', function (Blueprint $table) {
$table->dropColumn('email');
});
}
Run it:
php artisan migrate
Voilà! Your table is updated with the new column.
🧩 Migration Tips & Best Practices
| ✅ Do This | ❌ Avoid This |
|---|---|
Name migrations clearly (e.g., create_users_table) |
Using vague names like change_table1 |
Use php artisan make:migration |
Manually editing migration files without structure |
Add rollback logic in down() |
Leaving down() empty – it breaks rollbacks |
| Version-control your migration files | Sharing DB dumps only – hard to maintain |
📦 Pro Tip: Use Factories + Seeders
Combine migrations with seeders and model factories for test data:
php artisan make:seeder EmployeeSeeder
php artisan db:seed
🛡️ Laravel 12 Enhancements in Migrations
Laravel 12 makes it easier with:
- Anonymous class migrations (default)
- More type-safe definitions
- Improved error messages
- Cleaner
migrate:freshprocess
These improvements help you avoid common pitfalls and write more stable migrations.
🧪 Final Thoughts
Migrations are essential for team collaboration, testing, and deploying applications with confidence.
With Laravel's powerful tools, managing your database schema becomes:
- Safer
- Faster
- Reproducible
📚 What’s Next?
Once you're confident with migrations, dive into:
- Model Relationships (One-to-many, many-to-many)
- Database Indexing
- Raw SQL vs Schema Builder
- Testing with In-Memory Databases
🙌 Have Questions?
Got stuck or need help with a complex migration? Drop your question in the comments!
Projects related to this guide
Use the concepts from this article in practical source-code projects.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure...
Python Flask Blog Project with Admin Panel
Are you ready to launch a powerful, modern, and fully customizable blog? Look no further! Introducing the Flask-...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes pr...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.