Skip to content

Commit 04888df

Browse files
committed
fallback to primary key if owner key doesnt exist on model at all
1 parent 8638ab1 commit 04888df

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Eloquent/Relations/MorphTo.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,14 @@ protected function matchToMorphParents($type, Collection $results)
231231
*/
232232
public function associate($model)
233233
{
234+
if ($model instanceof Model) {
235+
$foreignKey = $this->ownerKey && $model->{$this->ownerKey}
236+
? $this->ownerKey
237+
: $model->getKeyName();
238+
}
239+
234240
$this->parent->setAttribute(
235-
$this->foreignKey, $model instanceof Model ? $model->{$this->ownerKey ?: $model->getKeyName()} : null
241+
$this->foreignKey, $model instanceof Model ? $model->{$foreignKey} : null
236242
);
237243

238244
$this->parent->setAttribute(

0 commit comments

Comments
 (0)