I’m creating "Foo" models using a Laravel 10 factory. I want each model Foo to have a belongsTo relationship to a random already existing model "Bar". There is no factory for Bar and I want to minimize the number of database queries.
I found a simple solution but it required to fetch all the Bar models for every created Foo and I wanted to avoid unnecessary queries.
The only way I found is to use states with the Bar models collection :
$bars = Bar::all();
Foo::factory()
->count(3)
->state(function () use ($bars) {
return ['bar_id' => $bars->random()->id];
})
Isn’t there a simpler way to do it?
2
Answers
In Laravel, you can achieve this by using a closure in your factory state. However, since you want to minimize the number of database queries, you can first fetch all the
Bar
models and then use a closure to select a randomBar
from the collection for eachFoo
you create. Here’s how you can do it:This code fetches all the
Bar
models and then, for eachFoo
being created, selects a randomBar
from the collection without any additional database queries. This approach minimizes the number of queries and achieves your desired result of having eachFoo
model belong to a randomBar
model.you can solve this issue by generating random
bar_id
values within the factory itself. Here’s how you can do it: