Skip to content

Commit cc69736

Browse files
committed
Merge pull request laravel#8706 from rkgrep/patch-morph-fon
[5.0] Fix relations firstOrNew logic
2 parents 0766a56 + 17ee7b3 commit cc69736

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ public function firstOrNew(array $attributes)
643643
{
644644
if (is_null($instance = $this->where($attributes)->first()))
645645
{
646-
$instance = $this->related->newInstance();
646+
$instance = $this->related->newInstance($attributes);
647647
}
648648

649649
return $instance;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function firstOrNew(array $attributes)
125125
{
126126
if (is_null($instance = $this->where($attributes)->first()))
127127
{
128-
$instance = $this->related->newInstance();
128+
$instance = $this->related->newInstance($attributes);
129129

130130
// When saving a polymorphic relationship, we need to set not only the foreign
131131
// key, but also the foreign key type, which is typically the class name of

tests/Database/DatabaseEloquentMorphTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testFirstOrNewMethodReturnsNewModelWithMorphKeysSet()
110110
$relation = $this->getOneRelation();
111111
$relation->getQuery()->shouldReceive('where')->once()->with(array('foo'))->andReturn($relation->getQuery());
112112
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
113-
$relation->getRelated()->shouldReceive('newInstance')->once()->with()->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
113+
$relation->getRelated()->shouldReceive('newInstance')->once()->with(array('foo'))->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
114114
$model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
115115
$model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
116116
$model->shouldReceive('save')->never();
@@ -161,7 +161,7 @@ public function testUpdateOrCreateMethodCreatesNewMorphModel()
161161
$relation = $this->getOneRelation();
162162
$relation->getQuery()->shouldReceive('where')->once()->with(array('foo'))->andReturn($relation->getQuery());
163163
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
164-
$relation->getRelated()->shouldReceive('newInstance')->once()->with()->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
164+
$relation->getRelated()->shouldReceive('newInstance')->once()->with(array('foo'))->andReturn($model = m::mock('Illuminate\Database\Eloquent\Model'));
165165
$model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
166166
$model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
167167
$model->shouldReceive('save')->once()->andReturn(true);

0 commit comments

Comments
 (0)