Database & EloquentExtra· 40 min read

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:

Generate a factory for the Product model
php artisan make:factory ProductFactory --model=Product

This 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:

The recipe for one fake product
// 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:

Create 50 fake products in one line
// 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:

Run seeders (and optionally rebuild the database first)
php artisan db:seed                 # run the seeders
php artisan migrate:fresh --seed    # rebuild all tables, then seed

Note: 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:

  1. Run php artisan make:factory ProductFactory --model=Product to create the factory file.
  2. Fill in definition() with $this->faker values describing one fake record.
  3. In DatabaseSeeder’s run() method, call Product::factory()->count(50)->create().
  4. Run php artisan db:seed (or migrate:fresh --seed to rebuild and reseed).
  5. 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?

Answer: A factory describes how to build one fake record (using Faker). A seeder then runs the factory to fill a table — and tests use factories too.

✍️ Practice

  1. Generate a factory for a model and define realistic fake fields.
  2. Seed 30 records with factory()->count(30)->create() and run migrate:fresh --seed.

🏠 Homework

  1. Write factories and a seeder for two related tables (e.g. users and posts) so seeding creates posts owned by users.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →