How to Seed Database in Laravel: Simple Guide with Examples
In Laravel, you seed the database by creating seed classes with
php artisan make:seed and defining data insertion logic inside them. Then run php artisan db:seed to execute these seeders and populate your database with test or initial data.Syntax
Laravel uses seed classes to insert data into the database. You create a seeder class with php artisan make:seed SeederName. Inside the run() method, you write code to add data. Finally, you run php artisan db:seed to execute all seeders or specify one with --class=SeederName.
php artisan make:seed SeederName: Creates a new seeder class.run()method: Contains the data insertion logic.php artisan db:seed: Runs all seeders registered inDatabaseSeeder.--class=SeederName: Runs a specific seeder.
bash/php
php artisan make:seed UsersTableSeeder // In database/seeders/UsersTableSeeder.php public function run() { DB::table('users')->insert([ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => bcrypt('password'), ]); } // Run all seeders php artisan db:seed // Run specific seeder php artisan db:seed --class=UsersTableSeeder
Example
This example shows how to create a seeder to add a user to the users table and run it.
php
<?php namespace Database\Seeders; 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 Smith', 'email' => 'alice@example.com', 'password' => Hash::make('secret123'), ]); } } // Then run: // php artisan db:seed --class=UsersTableSeeder
Output
Database seeded: 1 user added to users table
Common Pitfalls
- Not registering your seeder in
DatabaseSeeder.phpif you want it to run withphp artisan db:seed. - Forgetting to hash passwords before inserting.
- Running seeders without migrating the database first.
- Using
DB::table()->insert()without clearing existing data if duplicates are a concern.
php
<?php // Wrong: Password stored as plain text DB::table('users')->insert([ 'name' => 'Bob', 'email' => 'bob@example.com', 'password' => 'password123', // Not hashed ]); // Right: Password hashed DB::table('users')->insert([ 'name' => 'Bob', 'email' => 'bob@example.com', 'password' => Hash::make('password123'), ]);
Quick Reference
| Command | Description |
|---|---|
| php artisan make:seed SeederName | Create a new seeder class |
| php artisan db:seed | Run all seeders registered in DatabaseSeeder |
| php artisan db:seed --class=SeederName | Run a specific seeder |
| DB::table('table')->insert([...]) | Insert data inside seeder's run() method |
| Hash::make('password') | Hash passwords before inserting |
Key Takeaways
Create seeders with php artisan make:seed and define data in the run() method.
Run seeders using php artisan db:seed or specify a seeder with --class option.
Always hash passwords before inserting user data.
Register seeders in DatabaseSeeder.php to run them all together.
Run migrations before seeding to ensure tables exist.