You can get creative with a Sequence index, e.g.
User::factory()
->has(
Experience::factory()
->count(10)
->sequence(fn ($sequence) => ['end_date' => ($sequence->index + 1) < 10 ? now() : null])
)->create();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
In my user seeder, I want to create 10 experiences for each user. Now for one experience (at least) I need to set a end_date null. I am not sure how to do this.
Experience::factory()->create([
'user_id' => $user->id,
'end_date' => null, // 9 out of 10 experiences will have a end_date now() and 1 will have a null end_date
]);
Any suggestion?
@paypip you can use the collect helper function also, for example:
collect(range(1, 10))->each(function ($index) use ($user) {
Experience::factory()->create([
'user_id' => $user->id,
'end_date' => $index === 1 ? null : now(),
]);
});
Please or to participate in this conversation.