Skip to content

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
@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.

4 participants