Skip to main content

Introduction

When testing your application or seeding your database, you need to insert records into the database. Instead of manually specifying the value of each column, Laravel lets you define a set of default attributes for each Eloquent model using model factories. All new Laravel applications include database/factories/UserFactory.php as a starting example.
Via the fake() helper, factories have access to the Faker PHP library for generating random data.
Change your application’s Faker locale by updating the faker_locale option in config/app.php.

Generating factories

Create a factory using the make:factory Artisan command:
The new factory class is placed in your database/factories directory.

Model and factory discovery conventions

When your model uses the HasFactory trait, Laravel automatically finds the corresponding factory by looking in the Database\Factories namespace for a class named after the model suffixed with Factory. If the naming convention doesn’t apply, use the UseFactory attribute to specify the factory explicitly:

Defining factories

The definition() method

The definition() method returns the default attribute values for the model.
Commonly used Faker methods:

Factory states

State methods let you define discrete modifications that can be applied to your factories in any combination.
Apply states when instantiating the factory:

Trashed state

For soft-deletable models, use the built-in trashed() state:

Factory callbacks

Register afterMaking and afterCreating callbacks to perform additional tasks after the model is made or created.
You can also register callbacks inside state methods for state-specific side effects:

Creating models

make() — without persisting

create() — persists to the database

Overriding attributes

Pass an array to make() or create() to override specific attributes:
You can also override inline using state():
Mass assignment protection is automatically disabled when creating models using factories.

Sequences

Use Sequence to alternate attribute values across multiple generated models:
The sequence() method is a shorthand:
Use a closure to compute values dynamically. The $sequence->index property contains the current iteration count:

Factory relationships

Has many relationships

Use the has() method to create related models for a “has many” relationship:
Laravel’s magic methods let you write this more concisely:

Belongs to relationships

Use the for() method to define the parent model for a “belongs to” relationship:
Magic method version:

Many to many relationships

Use hasAttached() to create pivot table records alongside the related models:
Magic method version:

Polymorphic relationships

Polymorphic “morph many” relationships work the same as “has many”:
Magic methods cannot be used for morphTo relationships. Use for() with the relationship name explicitly:

Defining relationships within factories

In definition(), assign a factory instance to a foreign key to automatically create the parent model:

Recycling an existing model

Use recycle() to reuse a single model instance across all relationships created by the factory:

Using factories in seeders

Call factories from DatabaseSeeder or any seeder class:
Run the seeders:

Using factories in tests

Combine factories with the RefreshDatabase trait to reset the database between tests:
RefreshDatabase resets the database after each test so tests do not interfere with each other.
The factory API is identical when using Pest. Use uses(RefreshDatabase::class) to reset the database between tests.

Next steps

Database seeding

Learn how to combine seeders and factories to populate the database with initial data.

Testing

Explore Laravel’s testing tools and how RefreshDatabase works under the hood.
Last modified on April 12, 2026