I'm a little confused about Factories in Laravel.
Factories as all talk about is the ability to create dummies objects, so you can test, or just quickly generate dummy objects for your tests.
You usually use Faker
helper to get random data.
Now, I have another frequent use case that require factories, it is object creation.
So, for example, In TreeController@store
, I have a static method to create / update settings :
$championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);
with
public static function createOrUpdate(Request $request, Championship $championship): ChampionshipSettings
{
$request->request->add(['championship_id' => $championship->id]);
$arrSettings = $request->except('_token', 'numFighters');
$settings = static::where(['championship_id' => $championship->id])->first();
if ($settings == null) {
$settings = new self();
}
$settings->fill($arrSettings);
$settings->save();
return $settings;
}
I guess I would use Factories to manage object creation, but I can't because I already use them for dummy content.
Also, I could use different case in my factories, but it start incrementing complexity that I think I could avoid.
Then, I could use the existing factory, but if I don't specify an attribute, it will generate a random one, so I would need to set all unused attributes to null before creation...
Well, I'm kind of confused about how should I manage my factories...
via Juliatzin del Toro