Seeders & Model Factories
Fill your database with realistic fake data in one command — so you can test and demo without typing rows by hand.
What you will learn
- Generate fake records with a model factory
- Insert them with a seeder
- Run everything with one artisan command
Why fake data?
When you build an app you constantly need data to look at — a list of products, a page of users, comments under a post. Typing those in by hand through a form is slow and boring, and everyone on your team ends up with a different database. Seeders and factories solve this: they generate realistic fake data automatically.
- A factory is a recipe for making one fake record — for example "a product has a random name and a random price".
- A seeder is a script that runs factories (or inserts fixed rows) to actually fill a table.
- Faker is the built-in library that invents the fake values — real-looking names, sentences, prices, emails.
Step 1 — generate a factory
Artisan creates the factory file for you. This command makes a ProductFactory tied to the Product model:
php artisan make:factory ProductFactory --model=ProductThis creates database/factories/ProductFactory.php. The --model=Product part tells Laravel which model the factory builds, so it already knows the table.
Step 2 — describe one fake record
Inside the factory is a definition() method that returns an array — the same shape you would pass to Product::create(). Each value uses $this->faker to invent something realistic:
// database/factories/ProductFactory.php
public function definition(): array
{
return [
'name' => $this->faker->words(2, true),
'price' => $this->faker->randomFloat(2, 50, 999),
'in_stock' => $this->faker->boolean(80),
];
}Reading each line: $this->faker->words(2, true) makes a two-word product name like "silver keyboard". randomFloat(2, 50, 999) makes a price with 2 decimals somewhere between 50 and 999, e.g. 734.20. boolean(80) returns true about 80% of the time, so most products are in stock. Every time this recipe runs it produces different values — that is the point.
Step 3 — write a seeder that uses the factory
A seeder calls the factory and asks for as many records as you want. The run() method is where you do that:
// database/seeders/DatabaseSeeder.php
public function run(): void
{
Product::factory()->count(50)->create();
}Product::factory() grabs the factory you wrote, ->count(50) says "make fifty of them", and ->create() saves them all to the database. One line, fifty realistic rows.
Step 4 — run it
Finally you run the seeder from the terminal:
php artisan db:seed # run the seeders
php artisan migrate:fresh --seed # rebuild all tables, then seedNote: Output:
INFO Seeding database.
Database\Seeders\DatabaseSeeder ......... 312ms DONE
Your products table now holds 50 fake products. migrate:fresh --seed is the handy "start clean" command — it drops every table, re-runs your migrations, then seeds, giving everyone on the team an identical fresh database.
The whole workflow in order:
- Run
php artisan make:factory ProductFactory --model=Productto create the factory file. - Fill in
definition()with$this->fakervalues describing one fake record. - In
DatabaseSeeder’srun()method, callProduct::factory()->count(50)->create(). - Run
php artisan db:seed(ormigrate:fresh --seedto rebuild and reseed). - Refresh your app — it is now full of realistic data to work with.
Tip: Factories are not just for seeding — they are essential in tests, where each test creates exactly the records it needs with Product::factory()->create(). You will see this in the testing lesson.
Q. What is the job of a model factory?
✍️ Practice
- Generate a factory for a model and define realistic fake fields.
- Seed 30 records with
factory()->count(30)->create()and runmigrate:fresh --seed.
🏠 Homework
- Write factories and a seeder for two related tables (e.g. users and posts) so seeding creates posts owned by users.