You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Model factories contain the methods make and makeOne which do generate a model without creating a database record.
However, when the definition contains a relation to another factory. It always defaults to using create for the relation.
If often results in accidentally slowing down tests by creating unnecessary database records.
Before I create any Pull request with the required changes, I would like some input. And maybe I've missed some existing way to not have this issue at all.
Example definition for "TenderFactory":
public function definition(): array
{
return [
'title' => $this->faker->sentence,
'client_id' => ClientFactory::new(),
'status' => TenderStatus::DRAFT,
];
}
One way to fix it is by writing ClientFactory::new()->makeOne(), but then using create on the TenderFactory will break.
My suggestion would be to switch "mode" from create to make for the relations when make or makeOne is called.
For our own small project, I've already made a working proof of concept using the following changes:
In src/Illuminate/Database/Eloquent/Factories/Factory.php
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Model factories contain the methods
make
andmakeOne
which do generate a model without creating a database record.However, when the definition contains a relation to another factory. It always defaults to using
create
for the relation.If often results in accidentally slowing down tests by creating unnecessary database records.
Before I create any Pull request with the required changes, I would like some input. And maybe I've missed some existing way to not have this issue at all.
Example definition for "TenderFactory":
One way to fix it is by writing
ClientFactory::new()->makeOne()
, but then usingcreate
on the TenderFactory will break.My suggestion would be to switch "mode" from
create
tomake
for the relations whenmake
ormakeOne
is called.For our own small project, I've already made a working proof of concept using the following changes:
In src/Illuminate/Database/Eloquent/Factories/Factory.php
Some questions and notes I have
Beta Was this translation helpful? Give feedback.
All reactions