How to Use Seeder in Laravel: Simple Guide with Examples
In Laravel, use
php artisan make:seeder to create a seeder class, then define data insertion logic inside its run() method. Run php artisan db:seed to execute seeders and populate your database with sample data.Syntax
To create a seeder, run php artisan make:seeder SeederName. This creates a class in database/seeders. Inside the seeder class, use the run() method to add data to your database using Laravel's query builder or Eloquent models. Finally, run php artisan db:seed to execute all seeders or specify one with --class=SeederName.
bash
php artisan make:seeder SeederName // Seeder class example namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class SeederName extends Seeder { public function run() { DB::table('table_name')->insert([ 'column1' => 'value1', 'column2' => 'value2', ]); } } // Run all seeders php artisan db:seed // Run specific seeder php artisan db:seed --class=SeederName
Example
This example creates a seeder that inserts three users into the users table using Laravel's Eloquent model. It demonstrates how to generate the seeder, write the run() method, and run the seeder to add data.
php
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use App\Models\User; class UserSeeder extends Seeder { public function run() { User::insert([ ['name' => 'Alice', 'email' => 'alice@example.com', 'password' => bcrypt('password')], ['name' => 'Bob', 'email' => 'bob@example.com', 'password' => bcrypt('password')], ['name' => 'Charlie', 'email' => 'charlie@example.com', 'password' => bcrypt('password')], ]); } } // Run this seeder with: // php artisan db:seed --class=UserSeeder
Output
3 new users added to the users table
Common Pitfalls
- Forgetting to register the seeder in
DatabaseSeeder.phpif you want it to run withphp artisan db:seedalone. - Not hashing passwords when inserting user data, which causes login failures.
- Running seeders without migrating the database first, leading to errors.
- Using
insert()without timestamps if your table requires them, causing errors.
php
<?php // Wrong: Plain password stored User::insert([ ['name' => 'Dave', 'email' => 'dave@example.com', 'password' => 'secret'], ]); // Right: Password hashed User::insert([ ['name' => 'Dave', 'email' => 'dave@example.com', 'password' => bcrypt('secret')], ]);
Quick Reference
| Command | Description |
|---|---|
| php artisan make:seeder SeederName | Create a new seeder class |
| php artisan db:seed | Run all registered seeders |
| php artisan db:seed --class=SeederName | Run a specific seeder |
| php artisan migrate:fresh --seed | Reset database and run all seeders |
| bcrypt('password') | Hash passwords before inserting |
Key Takeaways
Create seeders with php artisan make:seeder and define data in the run() method.
Always hash passwords before inserting user data in seeders.
Run seeders with php artisan db:seed or specify a seeder with --class option.
Register seeders in DatabaseSeeder.php to run them all together.
Run migrations before seeding to avoid database errors.