Skip to content

Commit

Permalink
Merge branch '6.x' of https://github.com/simoebenhida/framework into …
Browse files Browse the repository at this point in the history
…simoebenhida-6.x
  • Loading branch information
taylorotwell committed Jan 20, 2020
2 parents c0607e2 + 41c40bc commit 37a70fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ public function create(array $attributes = [])
return $results;
}

/**
* Create a collection of models and persist them to the database.
*
* @param iterable $records
* @return mixed
*/
public function createMany(iterable $records)
{
$instances = array_map(function ($attribute) {
return $this->create($attribute);
}, $records);

return new Collection($instances);
}

/**
* Set the connection name on the results and store them.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/Database/EloquentFactoryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ public function testCreatingCollectionOfModels()
$this->assertCount(0, FactoryBuildableUser::find($instances->pluck('id')->toArray()));
}

public function testCreateManyCollectionOfModels()
{
$users = factory(FactoryBuildableUser::class)->createMany([
[
'name' => 'Taylor',
],
[
'name' => 'John',
],
[
'name' => 'Doe',
],
]);
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(3, $users);
$this->assertCount(3, FactoryBuildableUser::find($users->pluck('id')->toArray()));
$this->assertEquals(['Taylor', 'John', 'Doe'], $users->pluck('name')->toArray());
}

public function testCreatingModelsWithCallableState()
{
$server = factory(FactoryBuildableServer::class)->create();
Expand Down

0 comments on commit 37a70fd

Please sign in to comment.