Perhaps just seed and then do any testing in the query. An orderby query.
Feb 11, 2022
8
Level 63
How to seed with an 'order' field in a relationship ?
Hello,
I'd like to seed with fake data. A recipe has many steps and each step has an order number (1, 2, 3, ...).
Recipe::
factory()
->count(20)
->hasSteps(Arr::random([1, 2, 3, 4, 5, 6, 7]), function (array $attributes, Recipe $recipe) {
return [
'order' => // 1, 2, 3, 4, ... // => probably with a sequence but I really don't see how to do
'recipe_id' => $recipe->id,
];
})
->create();
Do you have any idea how to seed my order field ?
Thanks ;)
V
Level 3
You can use saveMany : see bellow
Recipe::factory()->count(20)->hasSteps(Arr::random([1, 2, 3, 4, 5, 6, 7]), function (array $attributes, Recipe $recipe) {
return [
'order' => $order
'recipe_id' => $recipe->id,
];
})->create()->each(function ($order) {
$Recipe->orders()->saveMany(factory(Orders::class, 5)->make());
Let me know if it works :0
Please or to participate in this conversation.