#Factory for() method creates duplicate parent model for all entries.

11 messages · Page 1 of 1 (latest)

jade trench
#

Hello there! I'm trying to create a Brand model for each CompanyBrand created, but this block of code create duplicated Brand models for many CompanyBrand models.

Could anyone take a look at it see if I'm doing something wrong please, and thank you in advance!

Company::factory()
              ->has(CompanyBrand::factory()->for(Brand::factory()->has(BrandActivity::factory(), 'activity')), 'brands')
              ->count(5)
              ->create();
elder meadow
#

Hard to say without knowing what’s going on in all of those factories.

jade trench
#
public function definition()
   {
      $name           = Str::replace('-', ' ', fake()->company());
      $form           = CompanyLegalForm::inRandomOrder()->first();
      $city           = City::inRandomOrder()->first();
      $rangeTurnover  = RangeTurnover::inRandomOrder()->first();
      $rangeEmployees = RangeEmployee::inRandomOrder()->first();

      return [
         'name'               => $name,
         'slug'               => Str::slug($name),
         'code'               => 'MA'. rand(1000000, 9999999),
         'country_id'         => 1,
         'legal_form_id'      => $form->id,
         'creation_date'      => fake()->date(),
         'tax'                => fake()->randomNumber(5, true),
         'register_trade'     => fake()->randomNumber(5, true),
         'creation_city'      => $city->id,
         'capital'            => fake()->randomNumber(6, true),
         'range_turnover_id'  => $rangeTurnover->id,
         'range_employee_id'  => $rangeEmployees->id,
         'setup_step'         => 3,
         'is_setup_complete'  => true,
      ];
   }
#

This is the company factory

#
public function definition()
   {
      return [];
   }
#

CompanyBrand

#
public function definition()
   {
      $brand = fake()->catchPhrase();

      return [
         'name'   => $brand,
         'slug'   => Str::slug($brand),
      ];
   }
#

BrandFactory

#
public function definition()
   {
      $activity = Activity::inRandomOrder()->first();

      return [
         'activity_id'  => $activity->id,
      ];
   }
#

BrandActivityFactory