Summer Sale! All accounts are 50% off this week.

vincent15000's avatar

recycle() method in factories

Hello,

I have this code in a seeder.

public function run(): void
{
    $companies = Company::all();

    Theme::withoutEvents(function () use ($companies) {
        Theme::factory()->recycle($companies)->count(50)->create();
    });
}

I get an error message saying that the company_id hasn't a default value.

What am I doing wrong with the recycle() method ?

https://laravel.com/docs/10.x/eloquent-factories#recycling-an-existing-model-for-relationships

Thanks for your help.

V

0 likes
2 replies
s4muel's avatar
s4muel
Best Answer
Level 50

how does your Theme factory definition look like? do you have company_id defined like this?

return [
    //...
    'company_id' => Company::factory(),
];

and make sure you have at least one Company available at the time when you run $companies = Company::all();

2 likes
vincent15000's avatar

@s4muel Oh I haven't defined this field, I thought it was automatically defined via the recycle() method.

1 like

Please or to participate in this conversation.