Migration vs Seeder in Laravel: Key Differences and Usage
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.
| Aspect | Migration | Seeder |
|---|---|---|
| Purpose | Create or modify database tables and columns | Insert sample or initial data into tables |
| Focus | Database schema structure | Database content/data |
| Typical Use | Define tables, indexes, foreign keys | Add default or test records |
| Run Command | php artisan migrate | php artisan db:seed |
| File Location | database/migrations | database/seeders |
| Effect | Changes database layout | Populates 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 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'); } }
Seeder Equivalent
This seeder inserts three sample users into the users table created by the migration.
<?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')], ]); } }
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.