Skip to content

Commit 036f0a0

Browse files
[10.x] createMany & createManyQuietly make argument optional (#48070)
* Add null as possible argument to createMany * Update Factory.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent c1e6281 commit 036f0a0

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,15 @@ public function createOneQuietly($attributes = [])
235235
/**
236236
* Create a collection of models and persist them to the database.
237237
*
238-
* @param int|iterable<int, array<string, mixed>> $records
238+
* @param int|null|iterable<int, array<string, mixed>> $records
239239
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
240240
*/
241-
public function createMany(int|iterable $records)
241+
public function createMany(int|iterable|null $records = null)
242242
{
243+
if (is_null($records)) {
244+
$records = $this->count ?? 1;
245+
}
246+
243247
if (is_numeric($records)) {
244248
$records = array_fill(0, $records, []);
245249
}
@@ -254,10 +258,10 @@ public function createMany(int|iterable $records)
254258
/**
255259
* Create a collection of models and persist them to the database without dispatching any model events.
256260
*
257-
* @param int|iterable<int, array<string, mixed>> $records
261+
* @param int|null|iterable<int, array<string, mixed>> $records
258262
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
259263
*/
260-
public function createManyQuietly(int|iterable $records)
264+
public function createManyQuietly(int|iterable|null $records = null)
261265
{
262266
return Model::withoutEvents(function () use ($records) {
263267
return $this->createMany($records);

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ public function test_basic_model_can_be_created()
130130
$this->assertInstanceOf(Collection::class, $users);
131131
$this->assertCount(2, $users);
132132

133+
$users = FactoryTestUserFactory::times(2)->createMany();
134+
$this->assertInstanceOf(Collection::class, $users);
135+
$this->assertCount(2, $users);
136+
137+
$users = FactoryTestUserFactory::new()->createMany();
138+
$this->assertInstanceOf(Collection::class, $users);
139+
$this->assertCount(1, $users);
140+
133141
$users = FactoryTestUserFactory::times(10)->create();
134142
$this->assertCount(10, $users);
135143
}

types/Database/Eloquent/Factories/Factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ public function definition()
7676
[['string' => 'string']]
7777
));
7878
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(3));
79+
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany());
7980

8081
// assertType('Illuminate\Database\Eloquent\Collection<int, User>', $factory->createManyQuietly([['string' => 'string']]));
8182
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly(
8283
[['string' => 'string']]
8384
));
8485
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly(3));
86+
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly());
8587

8688
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->create());
8789
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->create([

0 commit comments

Comments
 (0)