0
0
LaravelComparisonBeginner · 4 min read

Migration vs Seeder in Laravel: Key Differences and Usage

In Laravel, migrations define and modify the database structure like tables and columns, while seeders insert sample or initial data into those tables. Migrations manage schema changes, and seeders populate data for testing or setup.
⚖️

Quick Comparison

This table summarizes the main differences between Laravel migrations and seeders.

AspectMigrationSeeder
PurposeCreate or modify database tables and columnsInsert sample or initial data into tables
FocusDatabase schema structureDatabase content/data
Typical UseDefine tables, indexes, foreign keysAdd default or test records
Run Commandphp artisan migratephp artisan db:seed
File Locationdatabase/migrationsdatabase/seeders
EffectChanges database layoutPopulates data without changing schema
⚖️

Key Differences

Migrations are like blueprints for your database. They tell Laravel how to build or change tables, columns, and indexes. When you run migrations, Laravel updates the database structure to match your code. This helps keep your database organized and consistent across different environments.

Seeders, on the other hand, are like filling the rooms in a house with furniture. They add data to your tables, such as default users, settings, or sample content for testing. Seeders do not change the database layout; they only insert or update data inside the existing tables.

In short, use migrations to shape your database and seeders to fill it with useful data. Both work together to prepare your app's database for development and testing.

⚖️

Code Comparison

Here is an example of a migration that creates a users table with some columns.

php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}
Output
Creates a 'users' table with id, name, email, and timestamps columns in the database.
↔️

Seeder Equivalent

This seeder inserts three sample users into the users table created by the migration.

php
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->insert([
            ['name' => 'Alice', 'email' => 'alice@example.com', 'password' => Hash::make('password')],
            ['name' => 'Bob', 'email' => 'bob@example.com', 'password' => Hash::make('password')],
            ['name' => 'Charlie', 'email' => 'charlie@example.com', 'password' => Hash::make('password')],
        ]);
    }
}
Output
Inserts three user records into the 'users' table with hashed passwords.
🎯

When to Use Which

Choose migrations when you need to create or update your database structure, such as adding new tables or changing columns. They ensure your database schema stays consistent and version-controlled.

Choose seeders when you want to add default or test data to your database, like creating initial users or sample content. Seeders help you quickly populate your database for development or testing without altering its structure.

Key Takeaways

Migrations define and modify database tables and columns, managing schema changes.
Seeders insert sample or initial data into existing tables without changing structure.
Run migrations with 'php artisan migrate' and seeders with 'php artisan db:seed'.
Use migrations to shape your database and seeders to fill it with useful data.
Both tools work together to prepare your Laravel app's database for development.