Skip to content

Commit 0431475

Browse files
authored
set relation parent key when using forceCreate (#42281)
1 parent 934ec55 commit 0431475

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,19 @@ public function create(array $attributes = [])
297297
});
298298
}
299299

300+
/**
301+
* Create a new instance of the related model. Allow mass-assignment.
302+
*
303+
* @param array $attributes
304+
* @return \Illuminate\Database\Eloquent\Model
305+
*/
306+
public function forceCreate(array $attributes = [])
307+
{
308+
$attributes[$this->getForeignKeyName()] = $this->getParentKey();
309+
310+
return $this->related->forceCreate($attributes);
311+
}
312+
300313
/**
301314
* Create a Collection of new instances of the related model.
302315
*

tests/Database/DatabaseEloquentHasManyTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public function testCreateMethodProperlyCreatesNewModel()
5555
$this->assertEquals($created, $relation->create(['name' => 'taylor']));
5656
}
5757

58+
public function testForceCreateMethodProperlyCreatesNewModel()
59+
{
60+
$relation = $this->getRelation();
61+
$created = $this->expectForceCreatedModel($relation, ['name' => 'taylor']);
62+
63+
$this->assertEquals($created, $relation->forceCreate(['name' => 'taylor']));
64+
$this->assertEquals(1, $created->getAttribute('foreign_key'));
65+
}
66+
5867
public function testFindOrNewMethodFindsModel()
5968
{
6069
$relation = $this->getRelation();
@@ -304,6 +313,18 @@ protected function expectCreatedModel($relation, $attributes)
304313

305314
return $model;
306315
}
316+
317+
protected function expectForceCreatedModel($relation, $attributes)
318+
{
319+
$attributes[$relation->getForeignKeyName()] = $relation->getParentKey();
320+
321+
$model = m::mock(Model::class);
322+
$model->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey());
323+
324+
$relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($model);
325+
326+
return $model;
327+
}
307328
}
308329

309330
class EloquentHasManyModelStub extends Model

tests/Database/DatabaseEloquentHasOneTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ public function testCreateMethodProperlyCreatesNewModel()
129129
$this->assertEquals($created, $relation->create(['name' => 'taylor']));
130130
}
131131

132+
public function testForceCreateMethodProperlyCreatesNewModel()
133+
{
134+
$relation = $this->getRelation();
135+
$attributes = ['name' => 'taylor', $relation->getForeignKeyName() => $relation->getParentKey()];
136+
137+
$created = m::mock(Model::class);
138+
$created->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey());
139+
140+
$relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($created);
141+
142+
$this->assertEquals($created, $relation->forceCreate(['name' => 'taylor']));
143+
$this->assertEquals(1, $created->getAttribute('foreign_key'));
144+
}
145+
132146
public function testRelationIsProperlyInitialized()
133147
{
134148
$relation = $this->getRelation();

0 commit comments

Comments
 (0)