Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dougkira's avatar

Better solution for seeding

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 😉

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Welcome to the forum. Feel free to ask away :)

I would do it mostly the same way so it is completely fine. I assume this is just something you run to quickly set up your dev environment? So you just need to build it out when you add new stuff. And I don't know your project but can easily understand it

You are not forced to use factories, so if it makes no sense to have one now, then there is no need to make one

1 like
dougkira's avatar

@Sinnbeck thanks man!

Yes, it is for dev environment setup. I just i felt dummy thinking what if i need create 5 or 6 nested factories... i'd ending up making something like for(factory()->for(factory()->for(factory()->for(factory()->for())))) haha

Sinnbeck's avatar

@dougkira yeah I have seen that before and it looks cool. But it can also be quite hard to read. So here I would go for readability as well

1 like
dougkira's avatar

Seeing a @PovilasKorop i ended up using the top>down aproach and finally i found a way to not use a for() loop xD I ended up with this simple lines and working perfectly

foreach (['pdv', 'mdl', 'ca'] as $prefix) {
  ProductModelPrefix::create(['name' => $prefix]);
}

$allProductPrefixesIds = ProductModelPrefix::all()->pluck('id');

Category::factory(5)
  ->has(Product::factory(50)
    ->has(ProductModel::factory()->state(
      ['product_model_prefix_id' => $allProductPrefixesIds->random()]
    )
      ->for(ProductModelPrefix::factory(3))))
  ->create();

Hope this can help someone or myself in future haha and thanks again for your time @sinnbeck

PS: The code generate 5 categories with 50 products each and each product with your on model like: "pdf65487564", a total of 250 products.

Please or to participate in this conversation.