Can you show the global scope, the seeder and how you use it?
Global query scopes preventing seeding of model relationships
I am encountering a problem running a seeder on a model and its relationships. I have a global query scope that uses a session variable that does not exist in this context which causes the seeder to fail. Is there any way to bypass the global query scope when seeding? 'withoutGlobalScope' does not seem to work in seeders or factories or perhaps I'm not understanding how to use it in this context.
So here is a solution I came up with.
Investigation::factory(10)->create()->each(function ($investigation) {
Session::put('investigation', $investigation);
InvestigationContact::factory(5)->create();
});
The InvestigationContact factory then populates the investigation_id foreign key from the session variable.
alternatively
Investigation::factory(10)->create()->each(function ($investigation) {
Session::put('investigation', $investigation);
InvestigationContact::factory(5)->create([
'investigation_id' => $investigation->id,
]);
});
It's not as pretty as just using ->has('contact'), but it works and doesn't require messing with the query scope. It seems to me that bypassing global query scopes in seeders/factories might be a useful feature that could be added to Laravel.
Please or to participate in this conversation.