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 includedatabase/factories/UserFactory.php as a starting example.
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 themake:factory Artisan command:
database/factories directory.
Model and factory discovery conventions
When your model uses theHasFactory 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.
Factory states
State methods let you define discrete modifications that can be applied to your factories in any combination.Trashed state
For soft-deletable models, use the built-intrashed() state:
Factory callbacks
RegisterafterMaking and afterCreating callbacks to perform additional tasks after the model is made or created.
Creating models
make() — without persisting
create() — persists to the database
Overriding attributes
Pass an array tomake() or create() to override specific attributes:
state():
Mass assignment protection is automatically disabled when creating models using factories.
Sequences
UseSequence to alternate attribute values across multiple generated models:
sequence() method is a shorthand:
$sequence->index property contains the current iteration count:
Factory relationships
Has many relationships
Use thehas() method to create related models for a “has many” relationship:
Belongs to relationships
Use thefor() method to define the parent model for a “belongs to” relationship:
Many to many relationships
UsehasAttached() to create pivot table records alongside the related models:
Polymorphic relationships
Polymorphic “morph many” relationships work the same as “has many”:morphTo relationships. Use for() with the relationship name explicitly:
Defining relationships within factories
Indefinition(), assign a factory instance to a foreign key to automatically create the parent model:
Recycling an existing model
Userecycle() to reuse a single model instance across all relationships created by the factory:
Using factories in seeders
Call factories fromDatabaseSeeder or any seeder class:
Using factories in tests
Combine factories with theRefreshDatabase trait to reset the database between tests:
RefreshDatabase resets the database after each test so tests do not interfere with each other.
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.