Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] createMany & createManyQuietly make argument optional #48070

Merged
merged 2 commits into from Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
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|null|iterable $records = null)
driesvints marked this conversation as resolved.
Show resolved Hide resolved
{
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|null|iterable $records = null)
{
return Model::withoutEvents(function () use ($records) {
return $this->createMany($records);
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
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
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