Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,15 @@ public function createOneQuietly($attributes = [])
/**
* Create a collection of models and persist them to the database.
*
* @param int|iterable<int, array<string, mixed>> $records
* @param int|null|iterable<int, array<string, mixed>> $records
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
*/
public function createMany(int|iterable $records)
public function createMany(int|iterable|null $records = null)
{
if (is_null($records)) {
$records = $this->count ?? 1;
}

if (is_numeric($records)) {
$records = array_fill(0, $records, []);
}
Expand All @@ -254,10 +258,10 @@ public function createMany(int|iterable $records)
/**
* Create a collection of models and persist them to the database without dispatching any model events.
*
* @param int|iterable<int, array<string, mixed>> $records
* @param int|null|iterable<int, array<string, mixed>> $records
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
*/
public function createManyQuietly(int|iterable $records)
public function createManyQuietly(int|iterable|null $records = null)
{
return Model::withoutEvents(function () use ($records) {
return $this->createMany($records);
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ public function test_basic_model_can_be_created()
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(2, $users);

$users = FactoryTestUserFactory::times(2)->createMany();
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(2, $users);

$users = FactoryTestUserFactory::new()->createMany();
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(1, $users);

$users = FactoryTestUserFactory::times(10)->create();
$this->assertCount(10, $users);
}
Expand Down
2 changes: 2 additions & 0 deletions types/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ public function definition()
[['string' => 'string']]
));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(3));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany());

// assertType('Illuminate\Database\Eloquent\Collection<int, User>', $factory->createManyQuietly([['string' => 'string']]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly(
[['string' => 'string']]
));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly(3));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createManyQuietly());

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