Laravel Database Seeders and Fakers
Table of Contents
1. Introduction to Seeders and Fakers
When developing a Laravel application, you often need sample data in your database for testing and development. Instead of manually inserting rows into tables, Laravel provides Seeders and Factories (Fakers) to quickly generate realistic data.
-
Seeders → Insert test data into your database tables.
-
Factories (with Faker) → Automatically generate fake but realistic data like names, emails, phone numbers, and more.
This helps you:
-
Test your Laravel application with sample data.
-
Save time while developing.
-
Simulate real-world scenarios without using actual user data.
2. What is Database Seeding in Laravel?
Database Seeding means populating your database with initial or test data.
In Laravel, seeders are classes that contain instructions on what kind of data should be inserted into the database.
For example, you may want to add:
-
An admin user when the system installs.
-
Sample blog posts for testing.
-
Demo products in an e-commerce site.
3. What are Laravel Factories (Fakers)?
Factories use the Faker library to generate dummy data such as:
-
User names
-
Email addresses
-
Phone numbers
-
Profile pictures
-
Articles, posts, and more
Factories make testing and development faster by automatically creating hundreds of fake records with a single command.
4. Setting Up Database Seeders
Laravel already provides a main seeder file called DatabaseSeeder.php, which is located inside the database/seeders directory.
This file acts as the “master seeder” where you can register and call other seeders. By default, it looks like this:
👉 You can either directly add seed data inside this file or call other seeders from here.
5. Creating and Running a Seeder
Instead of writing all seed logic inside DatabaseSeeder.php, Laravel allows you to create separate seeder classes.
To create a new seeder, run:
This will generate a new file inside database/seeders/UserSeeder.php.
Example UserSeeder.php:
6- Update UserSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// Create a single admin user
User::create([
'name' => 'Admin User',
'email' => 'admin@example.com',
'password' => bcrypt('password'), // Always hash passwords
]);
}
}
6. Registering the Seeder in DatabaseSeeder
Once you create your custom seeder (like UserSeeder), you need to register it inside the main DatabaseSeeder.php file so that Laravel knows to execute it.
Open DatabaseSeeder.php and update the run() method:
public function run(): void
{
$this->call(UserSeeder::class);
}
This ensures that when you run the seeding command, Laravel will execute your UserSeeder.
7. Running the Seeder
To insert the seed data into your database, use the following Artisan command:
If you want to run a specific seeder, you can specify the class name:
✅ Now your database will have an Admin User created automatically.
8. Using Laravel Factories for Fake Data
Laravel provides factories to generate multiple fake records.
Create a factory:
This generates database/factories/UserFactory.php.
Example User Factory:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory
{
public function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'password' => bcrypt('password'),
];
}
}
Now generate 50 fake users using seeder:
9. Seeder and Factory Example
Let’s combine both.
UserSeeder.php:
public function run(): void
{
// Create 1 admin user
User::create([
'name' => 'Admin User',
'email' => 'admin@example.com',
'password' => bcrypt('password'),
]);
// Generate 50 fake users
User::factory()->count(10)->create();
}
Run seeder:
This will create one real admin and 10 fake users.

10. Seeding Relationships (Users, Posts, Comments)
Example: One User → Many Posts
PostFactory.php:
public function definition(): array
{
return [
'title' => $this->faker->sentence(),
'content' => $this->faker->paragraph(),
'user_id' => \App\Models\User::factory(),
];
}
DatabaseSeeder.php:
public function run(): void
{
\App\Models\User::factory()
->count(10)
->hasPosts(5)
->create();
}
This will create 10 users, each with 5 posts.
11. Running Multiple Seeders Together
Inside DatabaseSeeder.php:
Run all seeders at once:
12- Another Real World Example of Seeder & Fakers
Imagine you’re building an eCommerce app. You want to seed products with fake names, prices, and descriptions.
1. Create Seeder
php artisan make:seeder ProductSeeder
2. ProductSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Product;
class ProductSeeder extends Seeder
{
public function run(): void
{
// Create 50 fake products
Product::factory(50)->create();
}
}
3. ProductFactory.php (database/factories/ProductFactory.php)
4. Register Seeder in DatabaseSeeder.php
public function run(): void {
$this->call(ProductSeeder::class);
}
5. Run Seeder
This will create 50 fake products with realistic names, prices, and descriptions.
Best Practices for Laravel Seeders and Factories
-
Always create at least one real user/admin along with fake users.
-
Use factories to generate bulk fake data for testing.
-
Use unique() for fields like emails to prevent duplicate errors.
-
Group related seeders and call them in
DatabaseSeeder.php. -
Reset the database before seeding in local development:
Conclusion
Laravel Seeders and Factories (Fakers) are powerful tools that help developers create realistic demo data for testing and development.
-
Seeders insert fixed records like an admin user.
-
Factories use Faker to generate multiple fake users, posts, or products.
-
Together, they make development faster and testing easier.
By mastering seeders and factories, you’ll save hours of manual work and always have a ready-to-use database for your Laravel projects.