Skip to content
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

[10.x] createMany & createManyQuietly make argument optional #48070

Merged
merged 2 commits into from Aug 15, 2023

Conversation

JHWelch
Copy link
Contributor

@JHWelch JHWelch commented Aug 15, 2023

This builds on the changes from #48048. This allows createMany() to be called on a factory with no argument and still always return a collection of the model.

If the number of models has been specified with count($x) or times($x), that will be used to create $x many models.

If the number of models has not been specified, it will simply return a collection with a single model.

// Collection of 3 Users
User::factory()->count(3)->createMany();

// Collection of 1 User
User::factory()->createMany();

createMany is useful for static typing because it always returns an Eloquent Collection, similar to how the createOne method always returns a single model. create always requires a type check.

@utsavsomaiya
Copy link
Contributor

If you need wrap into collection...

Try this:
image

@taylorotwell taylorotwell merged commit 036f0a0 into laravel:10.x Aug 15, 2023
20 checks passed
@JHWelch JHWelch deleted the factory-createmany-accept-null branch August 15, 2023 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants