diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 4de065152d5b..600667428ad6 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -310,6 +310,19 @@ public function create(array $attributes = []) }); } + /** + * Create a new instance of the related model. Allow mass-assignment. + * + * @param array $attributes + * @return \Illuminate\Database\Eloquent\Model + */ + public function forceCreate(array $attributes) + { + $attributes[$this->getForeignKeyName()] = $this->getParentKey(); + + return $this->related->forceCreate($attributes); + } + /** * Create a Collection of new instances of the related model. * diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index 0d68f8fa7e77..24c302584381 100755 --- a/tests/Database/DatabaseEloquentHasManyTest.php +++ b/tests/Database/DatabaseEloquentHasManyTest.php @@ -55,6 +55,15 @@ public function testCreateMethodProperlyCreatesNewModel() $this->assertEquals($created, $relation->create(['name' => 'taylor'])); } + public function testForceCreateMethodProperlyCreatesNewModel() + { + $relation = $this->getRelation(); + $created = $this->expectForceCreatedModel($relation, ['name' => 'taylor']); + + $this->assertEquals($created, $relation->forceCreate(['name' => 'taylor'])); + $this->assertEquals(1, $created->getAttribute('foreign_key')); + } + public function testFindOrNewMethodFindsModel() { $relation = $this->getRelation(); @@ -304,6 +313,18 @@ protected function expectCreatedModel($relation, $attributes) return $model; } + + protected function expectForceCreatedModel($relation, $attributes) + { + $attributes[$relation->getForeignKeyName()] = $relation->getParentKey(); + + $model = m::mock(Model::class); + $model->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey()); + + $relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($model); + + return $model; + } } class EloquentHasManyModelStub extends Model diff --git a/tests/Database/DatabaseEloquentHasOneTest.php b/tests/Database/DatabaseEloquentHasOneTest.php index c8d253d05524..2a8712319bc3 100755 --- a/tests/Database/DatabaseEloquentHasOneTest.php +++ b/tests/Database/DatabaseEloquentHasOneTest.php @@ -129,6 +129,20 @@ public function testCreateMethodProperlyCreatesNewModel() $this->assertEquals($created, $relation->create(['name' => 'taylor'])); } + public function testForceCreateMethodProperlyCreatesNewModel() + { + $relation = $this->getRelation(); + $attributes = ['name' => 'taylor', $relation->getForeignKeyName() => $relation->getParentKey()]; + + $created = m::mock(Model::class); + $created->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey()); + + $relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($created); + + $this->assertEquals($created, $relation->forceCreate(['name' => 'taylor'])); + $this->assertEquals(1, $created->getAttribute('foreign_key')); + } + public function testRelationIsProperlyInitialized() { $relation = $this->getRelation();