diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 2f449876fac7..8a6c7a2cbca8 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -226,7 +226,7 @@ protected function matchToMorphParents($type, Collection $results) public function associate($model) { $this->parent->setAttribute( - $this->foreignKey, $model instanceof Model ? $model->getKey() : null + $this->foreignKey, $model instanceof Model ? $model->{$this->ownerKey ?: $model->getKeyName()} : null ); $this->parent->setAttribute( diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index a6bd248ff563..0b0741de406c 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -132,7 +132,7 @@ public function testAssociateMethodSetsForeignKeyAndTypeOnModel() $relation = $this->getRelationAssociate($parent); $associate = m::mock(Model::class); - $associate->shouldReceive('getKey')->once()->andReturn(1); + $associate->shouldReceive('getAttribute')->once()->andReturn(1); $associate->shouldReceive('getMorphClass')->once()->andReturn('Model'); $parent->shouldReceive('setAttribute')->once()->with('foreign_key', 1);