Kritim Yantra
May 20, 2025
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.
“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:
This helps keep your schema in sync across all environments and developers.
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
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.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!
Mistakes happen. Laravel has your back!
php artisan migrate:rollback
php artisan migrate:reset
php artisan migrate:refresh
Pro tip: Add --seed
to also re-run seeders:
php artisan migrate:refresh --seed
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.
✅ 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 |
Combine migrations with seeders and model factories for test data:
php artisan make:seeder EmployeeSeeder
php artisan db:seed
Laravel 12 makes it easier with:
migrate:fresh
processThese improvements help you avoid common pitfalls and write more stable migrations.
Migrations are essential for team collaboration, testing, and deploying applications with confidence.
With Laravel's powerful tools, managing your database schema becomes:
Once you're confident with migrations, dive into:
Got stuck or need help with a complex migration? Drop your question in the comments!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google