From 2001a93dde426cc6a69165b4df06c76e4f2c9316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 8 Jun 2023 14:37:03 +0200 Subject: [PATCH 1/2] [10.x] Add getForeignKeyFrom method --- .../Database/Eloquent/Relations/BelongsTo.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index d7bfa0f60a17..984d603a2e6f 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -74,7 +74,7 @@ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey */ public function getResults() { - if (is_null($this->child->{$this->foreignKey})) { + if (is_null($this->getForeignKeyFrom($this->child))) { return $this->getDefaultFor($this->parent); } @@ -94,7 +94,7 @@ public function addConstraints() // of the related models matching on the foreign key that's on a parent. $table = $this->related->getTable(); - $this->query->where($table.'.'.$this->ownerKey, '=', $this->child->{$this->foreignKey}); + $this->query->where($table.'.'.$this->ownerKey, '=', $this->getForeignKeyFrom($this->child)); } } @@ -130,7 +130,7 @@ protected function getEagerModelKeys(array $models) // to query for via the eager loading query. We will add them to an array then // execute a "where in" statement to gather up all of those related records. foreach ($models as $model) { - if (! is_null($value = $model->{$this->foreignKey})) { + if (! is_null($value = $this->getForeignKeyFrom($model))) { $keys[] = $value; } } @@ -337,7 +337,7 @@ public function getQualifiedForeignKeyName() */ public function getParentKey() { - return $this->child->{$this->foreignKey}; + return $this->getForeignKeyFrom($this->child); } /** @@ -371,6 +371,17 @@ protected function getRelatedKeyFrom(Model $model) return $model->{$this->ownerKey}; } + /** + * Get the value of the model's associated key. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return mixed + */ + protected function getForeignKeyFrom(Model $model) + { + return $model->{$this->foreignKey}; + } + /** * Get the name of the relationship. * From d72430ddac2577f854b8d04855b711c35c86c6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 8 Jun 2023 14:37:58 +0200 Subject: [PATCH 2/2] fix comment --- src/Illuminate/Database/Eloquent/Relations/BelongsTo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 984d603a2e6f..f2ecbb3bf070 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -372,7 +372,7 @@ protected function getRelatedKeyFrom(Model $model) } /** - * Get the value of the model's associated key. + * Get the value of the model's foreign key. * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed