diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index d186e7e326bc..d9a453359d23 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -235,11 +235,15 @@ public function createOneQuietly($attributes = []) /** * Create a collection of models and persist them to the database. * - * @param int|iterable> $records + * @param int|null|iterable> $records * @return \Illuminate\Database\Eloquent\Collection */ - 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, []); } @@ -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> $records + * @param int|null|iterable> $records * @return \Illuminate\Database\Eloquent\Collection */ - public function createManyQuietly(int|iterable $records) + public function createManyQuietly(int|iterable|null $records = null) { return Model::withoutEvents(function () use ($records) { return $this->createMany($records); diff --git a/tests/Database/DatabaseEloquentFactoryTest.php b/tests/Database/DatabaseEloquentFactoryTest.php index 34eb1461008b..f46ebc3213f2 100644 --- a/tests/Database/DatabaseEloquentFactoryTest.php +++ b/tests/Database/DatabaseEloquentFactoryTest.php @@ -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); } diff --git a/types/Database/Eloquent/Factories/Factory.php b/types/Database/Eloquent/Factories/Factory.php index 583a194948ab..433dee6fb204 100644 --- a/types/Database/Eloquent/Factories/Factory.php +++ b/types/Database/Eloquent/Factories/Factory.php @@ -76,12 +76,14 @@ public function definition() [['string' => 'string']] )); assertType('Illuminate\Database\Eloquent\Collection', $factory->createMany(3)); +assertType('Illuminate\Database\Eloquent\Collection', $factory->createMany()); // assertType('Illuminate\Database\Eloquent\Collection', $factory->createManyQuietly([['string' => 'string']])); assertType('Illuminate\Database\Eloquent\Collection', $factory->createManyQuietly( [['string' => 'string']] )); assertType('Illuminate\Database\Eloquent\Collection', $factory->createManyQuietly(3)); +assertType('Illuminate\Database\Eloquent\Collection', $factory->createManyQuietly()); // assertType('Illuminate\Database\Eloquent\Collection|User', $factory->create()); // assertType('Illuminate\Database\Eloquent\Collection|User', $factory->create([