Skip to content

Commit 2d3fdc0

Browse files
authored
createMany & createManyQuietly add count argument (#48048)
1 parent e5e7704 commit 2d3fdc0

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-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 iterable<int, array<string, mixed>> $records
238+
* @param int|iterable<int, array<string, mixed>> $records
239239
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
240240
*/
241-
public function createMany(iterable $records)
241+
public function createMany(int|iterable $records)
242242
{
243+
if (is_numeric($records)) {
244+
$records = array_fill(0, $records, []);
245+
}
246+
243247
return new EloquentCollection(
244248
collect($records)->map(function ($record) {
245249
return $this->state($record)->create();
@@ -250,10 +254,10 @@ public function createMany(iterable $records)
250254
/**
251255
* Create a collection of models and persist them to the database without dispatching any model events.
252256
*
253-
* @param iterable<int, array<string, mixed>> $records
257+
* @param int|iterable<int, array<string, mixed>> $records
254258
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
255259
*/
256-
public function createManyQuietly(iterable $records)
260+
public function createManyQuietly(int|iterable $records)
257261
{
258262
return Model::withoutEvents(function () use ($records) {
259263
return $this->createMany($records);

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ public function test_basic_model_can_be_created()
126126
$this->assertInstanceOf(Collection::class, $users);
127127
$this->assertCount(2, $users);
128128

129+
$users = FactoryTestUserFactory::new()->createMany(2);
130+
$this->assertInstanceOf(Collection::class, $users);
131+
$this->assertCount(2, $users);
132+
129133
$users = FactoryTestUserFactory::times(10)->create();
130134
$this->assertCount(10, $users);
131135
}

types/Database/Eloquent/Factories/Factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ public function definition()
7575
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(
7676
[['string' => 'string']]
7777
));
78+
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(3));
7879

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

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

0 commit comments

Comments
 (0)