Skip to content

Commit 697345b

Browse files
authored
[10.x] Add getForeignKeyFrom method (#47378)
* [10.x] Add getForeignKeyFrom method * fix comment
1 parent 97ab967 commit 697345b

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey
7474
*/
7575
public function getResults()
7676
{
77-
if (is_null($this->child->{$this->foreignKey})) {
77+
if (is_null($this->getForeignKeyFrom($this->child))) {
7878
return $this->getDefaultFor($this->parent);
7979
}
8080

@@ -94,7 +94,7 @@ public function addConstraints()
9494
// of the related models matching on the foreign key that's on a parent.
9595
$table = $this->related->getTable();
9696

97-
$this->query->where($table.'.'.$this->ownerKey, '=', $this->child->{$this->foreignKey});
97+
$this->query->where($table.'.'.$this->ownerKey, '=', $this->getForeignKeyFrom($this->child));
9898
}
9999
}
100100

@@ -130,7 +130,7 @@ protected function getEagerModelKeys(array $models)
130130
// to query for via the eager loading query. We will add them to an array then
131131
// execute a "where in" statement to gather up all of those related records.
132132
foreach ($models as $model) {
133-
if (! is_null($value = $model->{$this->foreignKey})) {
133+
if (! is_null($value = $this->getForeignKeyFrom($model))) {
134134
$keys[] = $value;
135135
}
136136
}
@@ -337,7 +337,7 @@ public function getQualifiedForeignKeyName()
337337
*/
338338
public function getParentKey()
339339
{
340-
return $this->child->{$this->foreignKey};
340+
return $this->getForeignKeyFrom($this->child);
341341
}
342342

343343
/**
@@ -371,6 +371,17 @@ protected function getRelatedKeyFrom(Model $model)
371371
return $model->{$this->ownerKey};
372372
}
373373

374+
/**
375+
* Get the value of the model's foreign key.
376+
*
377+
* @param \Illuminate\Database\Eloquent\Model $model
378+
* @return mixed
379+
*/
380+
protected function getForeignKeyFrom(Model $model)
381+
{
382+
return $model->{$this->foreignKey};
383+
}
384+
374385
/**
375386
* Get the name of the relationship.
376387
*

0 commit comments

Comments
 (0)