Skip to content

[8.x] Use index or count property of Sequence object #7163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion database-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,17 @@ If necessary, you may include a closure as a sequence value. The closure will be
$users = User::factory()
->count(10)
->state(new Sequence(
fn () => ['role' => UserRoles::all()->random()],
fn ($sequence) => ['role' => UserRoles::all()->random()],
))
->create();

Within a sequence closure, you may access the `$index` or `$count` properties on the sequence instance that is injected into the closure. The `$index` property contains the number of iterations through the sequence that have occurred thus far, while the `$count` property contains the total number of times the sequence will be invoked:

$users = User::factory()
->count(10)
->sequence(fn ($sequence) => ['name' => 'Name '.$sequence->index])
->create();

<a name="factory-relationships"></a>
## Factory Relationships

Expand Down