Field Manual PHP / Laravel reference
← Laravel

Factories

Factories create variable test fixtures.

What it is

Factories create variable test fixtures.

When to use it

Use in tests when many records or states are useful.

Syntax

User::factory()->create();

Example

public function definition(): array
{
    return ["reference" => fake()->unique()->uuid(),
            "total_cents" => fake()->numberBetween(1, 100000),
            "status" => "pending"];
}

Common mistakes

Random fixtures can obscure boundary cases; specify important business values explicitly.

Related topics

Browse Laravel