You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed a subtle bug in the EloquentFactory method when it's using in conjunction with a factory count. The createMany should return an EloquentCollection of models (i.e., a flat structure):
However, when the factory has a count provided, a collection of collections is returned, and n^2 models are created:
// expected a collection of 3 models// returns a collection of 3 collections with 3 models each---9 records totalUser::factory()->count(3)->createMany();
I have a PR ready to go to fix this, but I just need some clarification on the expected behaviour when there's both a count and an argument passed to createMany:
User::factory()->count(2)->createMany(3); // ?
Should:
the createMany arg override the count (3 models returned)?
the count override the createMany arg (2 models returned)?
the counts be added together (5 models returned)?
the counts be multiplied together (6 models returned)?
Note that an iterable (states) could also be passed to createMany which would normally create that many records.
It may be more trouble than it's worth to try to bring the count into the mix, so it may be easiest just to have the createMany override the count (Option 1).
Laravel Version
10.x
PHP Version
8.2.x
Database Driver & Version
No response
Description
Hello!
I noticed a subtle bug in the EloquentFactory method when it's using in conjunction with a factory count. The
createMany
should return an EloquentCollection of models (i.e., a flat structure):framework/src/Illuminate/Database/Eloquent/Factories/Factory.php
Lines 235 to 241 in a435375
However, when the factory has a count provided, a collection of collections is returned, and n^2 models are created:
I have a PR ready to go to fix this, but I just need some clarification on the expected behaviour when there's both a count and an argument passed to
createMany
:Should:
createMany
arg override thecount
(3 models returned)?count
override thecreateMany
arg (2 models returned)?Note that an iterable (states) could also be passed to
createMany
which would normally create that many records.It may be more trouble than it's worth to try to bring the count into the mix, so it may be easiest just to have the
createMany
override thecount
(Option 1).Example:
Options:
createMany
Thanks!
Steps To Reproduce
See above
The text was updated successfully, but these errors were encountered: