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
Would it be possible to prevent factory states from colliding. This can be useful if you have multiple states that can not be combined.
For example the factory below:
<?php
namespace Database\Factories;
use App\Models\Project;
use App\Models\Project\Project as OtherProject;
use App\Models\Project\Bundle;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProjectFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Project::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition(): array
{
return [
'project_number' => rand( 10000, 99999 ),
'project_id' => null,
'bundle_id' => null,
];
}
/**
* If the project is related to a project
*
* @return Factory
*/
public function project(): Factory
{
$project = OtherProject::factory()->create();
return $this->state([
'project_number' => $project->number,
'project_id' => $project->id,
]);
}
/**
* If the project is related to a bundle
*
* @return Factory
*/
public function bundle(): Factory
{
$bundle = Bundle::factory()->create();
return $this->state([
'project_number' => $bundle->number,
'bundle_id' => $bundle->id
]);
}
}
The states project and bundle can't be combined. Would it be possible to add some sort of protected $colliding property. This property could look like:
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.
Uh oh!
There was an error while loading. Please reload this page.
-
Would it be possible to prevent factory states from colliding. This can be useful if you have multiple states that can not be combined.
For example the factory below:
The states
project
andbundle
can't be combined. Would it be possible to add some sort ofprotected $colliding
property. This property could look like:In a larger factory there could be more states that can't combine and will collide so in that case it would look like:
or
And of course if a colliding combination is used some error or exception should be thrown
Beta Was this translation helpful? Give feedback.
All reactions