Laravel 8 model factory using rand

Viewed 373

I have just updated my Laravel installation from 7 to 8 and I am trying to convert my old factories to the new class based factories.

I have the following Laravel 7 factory;

$bottles = factory(Bottle::class, rand(1, 5))->create([
   'email' => $faker->safeEmail
])

How would I convert this to Laravel 8. The rand part doesnt seem to be working, I tried;

$bottles = Bottle::factory()->rand(1, 5)->create([
    'email' => $this->faker->safeEmail
]);

But i get the following error;

Call to undefined method Database\Factories\BottleFactory::rand()
1 Answers

you can use count() method for the laravel8 factory:

   $bottles = Bottle::factory()->count(rand(1, 5))->create([
            'email' => $this->faker->safeEmail
        ]);
Related