Factories
- Using Factories to Generate Related Models
- Set Factory State
- Factory Relationships
- Additional Resources
Using Factories to Generate Related Models
When creating a model with relationships, you can use factories to generate related models.
return [
'started_at' => now(),
'user_id' => User::factory(), // Use the User factory to generate a user_id
];
Set Factory State
public function published(?Carbon $date = null): self
{
return $this->state(
fn (array $attr) => ['published_at' => $date ?? Carbon::now()]
);
}
Factory Relationships
Has Many Relationships
$course = Course::factory()
->has(Chapter::factory()->count(3))
->create();
Nested Relationships
$course = Course::factory()
->has(Chapter::factory()
->has(Media::factory()->count(3))
->count(3)
)
->create();
## Fake Data
### Numbers
#### `randomDigit`, `randomNumber`, `numberBetween`
```php
// an integer between 0 and 9
$this->faker->randomDigit();
// random integer with UP TO n digits (123, 43, 19238, 5, or 1203)
$this->faker->randomNumber(5, false);
// random integer with EXACTLY n digits (2643, 42931, or 32919)
$this->faker->randomNumber(5, true);
// integer between
'price' => $this->faker->numberBetween(500, 100000);
randomFloat
randomFloat($nbMaxDecimals = null, $min = 0, $max = null):
'price' => $this->faker->randomFloat(2),
'price' => $this->faker->randomFloat(2, 5, 30);
'price' => ceil($this->faker->randomFloat(2, 5, 10.50)*10)/10,
Unique Data
You can generate unique data simply by using the unique
method.
'code' => $this->faker->unique()->numberBetween(1000000, 9000000),
'code' => $this->faker->unique()->randomNumber()