Hello everyone, this is my first question so plz tell me if i shoudn't post here or broke some rules.
My main question Is, if is there any better solution to seed products table with nested relationships? My code does the job, but i feel like in some point i'll have deeper and deeper nested relationships, so the indentation with for(model::factory->create()) will ended up as a indentation hell haha
foreach (['pdv', 'mdl', 'ca'] as $prefix) {
ProductModelPrefix::create(['name' => $prefix]);
}
$allProductModelPrefixesIds = ProductModelPrefix::all()->pluck('id');
$allCategoryIds = Category::factory(10)->create()->pluck('id');
for ($i = 0; $i < 100; $i++) {
ProductModel::factory()
->state(['product_model_prefix_id' => $allProductModelPrefixesIds->random()])
->for(
Product::factory()
// ->forCategory() // forCategory = ->for(Category::factory()->create())
->state(['category_id' => $allCategoryIds->random()])
->create()
)
->create();
}
Another little doubt is if is there any future problem in not have a ProductModelFactory? I didn't make one because it will have only 3 values for now with just a "name" column, but it smells like the same problem of "magic numbers".
Thanks anyone for for reading 😉