When building a Laravel application, you often need sample data to test your features.
Instead of manually inserting records, Laravel provides seeding, a powerful way to populate your database with test data effortlessly. In this guide, we’ll explore what seeding is, why it’s useful, and how you can implement it in your Laravel project.
What is Laravel Seeding?
Seeding in Laravel allows you to insert dummy data into your database tables using a predefined script. This is particularly helpful during development and testing, as it lets you quickly generate sample users, posts, products, or any other data.
Creating a Seeder in Laravel
To create a seeder, run the following Artisan command:
php artisan make:seeder UserSeeder
This will generate a new seeder file in the database/seeders/
directory.
Open database/seeders/UserSeeder.php
and modify the run
method to insert test data:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserSeeder extends Seeder
{
public function run()
{
DB::table('users')->insert([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => Hash::make('password123'),
]);
}
}
This script will insert a dummy user into the users
table.
Running the Seeder
After defining the seeder, run it using:
php artisan db:seed --class=UserSeeder
This will execute the run()
method and insert the test user into the database.
To seed multiple seeders at once, register them in DatabaseSeeder.php
:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
UserSeeder::class,
]);
}
}
Then run:
php artisan db:seed
This will execute all registered seeders.
Using Faker for Dynamic Dummy Data
Instead of manually defining data, you can use Laravel’s built-in Faker library to generate random values.
Modify your seeder like this:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Faker\Factory as Faker;
class UserSeeder extends Seeder
{
public function run()
{
$faker = Faker::create();
for ($i = 0; $i < 10; $i++) {
DB::table('users')->insert([
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => Hash::make('password123'),
]);
}
}
}
Now, every time you seed, you’ll get 10 random users!
Factory-Based Seeding (Recommended for Eloquent Models)
Instead of using DB::table()
, you can seed data using Laravel Factories, which are more maintainable.
Step 1: Create a Factory
php artisan make:factory UserFactory --model=User
Open database/factories/UserFactory.php
and define the fields:
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
protected $model = \App\Models\User::class;
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'password' => Hash::make('password123'),
];
}
}
Step 2: Use the Factory in a Seeder
Modify UserSeeder.php
:
use Illuminate\Database\Seeder;
use App\Models\User;
class UserSeeder extends Seeder
{
public function run()
{
User::factory()->count(10)->create();
}
}
Now, run:
php artisan db:seed --class=UserSeeder
This will insert 10 users with realistic fake data!
Resetting and Seeding the Database
If you want to clear and reseed the database from scratch, use:
php artisan migrate:fresh --seed
This will:
- Drop all tables
- Re-run all migrations
- Seed the database with test data
Why Use Seeding?
✅ Saves time – No need to manually insert data every time you reset the database.
✅ Helps testing – You can quickly generate users, posts, products, etc. for testing.
✅ Keeps consistency – Everyone on the team has the same sample data.
Conclusion
Laravel seeding is a powerful feature that simplifies database population during development and testing. Whether you use manual data, Faker, or Eloquent Factories, it helps create a realistic dataset effortlessly.
Next time you start a project, set up your seeders early to save time and make testing smoother! 🚀