diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index bc547702aa7a..d015c863bf77 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -59,6 +59,23 @@ public function make(array $attributes = []) }); } + /** + * Create and return an un-saved instances of the related models. + * + * @param iterable $records + * @return \Illuminate\Database\Eloquent\Collection + */ + public function makeMany($records) + { + $instances = $this->related->newCollection(); + + foreach ($records as $record) { + $instances->push($this->make($record)); + } + + return $instances; + } + /** * Set the base constraints on the relation query. * diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index e09b04aaf52c..25c4aff370c2 100755 --- a/tests/Database/DatabaseEloquentHasManyTest.php +++ b/tests/Database/DatabaseEloquentHasManyTest.php @@ -26,6 +26,27 @@ public function testMakeMethodDoesNotSaveNewModel() $this->assertEquals($instance, $relation->make(['name' => 'taylor'])); } + public function testMakeManyCreatesARelatedModelForEachRecord() + { + $records = [ + 'taylor' => ['name' => 'taylor'], + 'colin' => ['name' => 'colin'], + ]; + + $relation = $this->getRelation(); + $relation->getRelated()->shouldReceive('newCollection')->once()->andReturn(new Collection); + + $taylor = $this->expectNewModel($relation, ['name' => 'taylor']); + $taylor->expects($this->never())->method('save'); + $colin = $this->expectNewModel($relation, ['name' => 'colin']); + $colin->expects($this->never())->method('save'); + + $instances = $relation->makeMany($records); + $this->assertInstanceOf(Collection::class, $instances); + $this->assertEquals($taylor, $instances[0]); + $this->assertEquals($colin, $instances[1]); + } + public function testCreateMethodProperlyCreatesNewModel() { $relation = $this->getRelation();