Good Afternoon, I'm looking for some help please with Laravel 10.
I am looking to write a database seeder which will use the Incidents
factory to generate new incidents.
Each Incident
should have additional models created called SkillUsed
during their creation.
Lets say the skill_id are:
1
, 2
, 3
.
The catch here is an Incident factory should not have duplicate skills. For example: 1,2 is fine but 1,1 is not.
Database Seeder:
protected function seedIncidents() : void {
// Add some incidents made-to-scene, excluding scheme vehicles and case reviews and no POCUS skill.
$incident = Incident::factory()->state([
'incident_snr' => 0,
])->count(50)->create()
->each(function($incident) {
SkillUsed::factory()
->count(fake()->numberBetween(1,5))
->create();
});
Skill Used Factory:
public function definition(array $attributes = []): array
{
$incident = $attributes['incident_id'] ?? Incident::inRandomOrder()->first()->value('incident_id');
$existingSkillsUsed = Incident::find($incident)->SkillsUsed()->pluck('sid')->toArray();
$skill = Skill::whereNotIn('sid', $existingSkillsUsed)->inRandomOrder()->first();
return [
'sid' => $skill->sid,
'incident_id' => $incident,
'deleted_at' => fake()->optional($weight = 0.2)->dateTimeBetween($startDate = '-6 months', $endDate = 'now', $timezone = null),
];
}
My problem is how to access the previously created Incident
model to ensure the Skill isn't duplicated.
Much appreciated :)