Dec 29, 2021
0
Level 1
Default many-to-many relationship in Factory
Hi,
I'm trying to save default value of my many-to-many relationship in case where my factory doesn't call the state function withConstructors.
I have a Model Building, a Model Constructor and a many-to-many relationship between both.
In my factory, I have a configure function with the code bellow:
public function configure(): array
{
return $this->afterCreating(function (Building $building) {
if ($building->constructors()->count() === 0) {
$building->constructors()->save(Contructor::factory()->create());
}
});
}
And a state function withConstructors with the code bellow:
public function withConstructors($con)
{
return $this->afterCreating(function (Building $building) use ($con) {
$building->constructors()->save($con);
});
}
I'm searching a way to call withContructors function before the afterCreating of configure function to avoid creation of dummy Constructor.
Do you have any idea to solv my problem ? Thanks
Please or to participate in this conversation.