From b52c53eac3c9fb8042abec9b592eb8224604779d Mon Sep 17 00:00:00 2001 From: Upperfoot Date: Wed, 6 Sep 2017 16:59:22 +0100 Subject: [PATCH 1/2] Resolve Eager Loading Problem with BelongsToMany This resolves the problem of the relation not making use of the parentKey for the parent Models. --- src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 8dfbb9bedf1b..b85e92675b32 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -194,7 +194,7 @@ protected function addWhereConstraints() */ public function addEagerConstraints(array $models) { - $this->query->whereIn($this->getQualifiedForeignPivotKeyName(), $this->getKeys($models)); + $this->query->whereIn($this->getQualifiedForeignPivotKeyName(), $this->getKeys($models, $this->parentKey)); } /** @@ -229,7 +229,7 @@ public function match(array $models, Collection $results, $relation) // children back to their parent using the dictionary and the keys on the // the parent models. Then we will return the hydrated models back out. foreach ($models as $model) { - if (isset($dictionary[$key = $model->getKey()])) { + if (isset($dictionary[$key = $model->{$this->parentKey}])) { $model->setRelation( $relation, $this->related->newCollection($dictionary[$key]) ); From 0dbf4879ac3ad7cca2669cb4922f715237ec32db Mon Sep 17 00:00:00 2001 From: Upperfoot Date: Wed, 6 Sep 2017 17:01:57 +0100 Subject: [PATCH 2/2] Completed BelongsToMany Parent Key Change This resolves the issue where BelongsToMany.php makes use of parentKey, but this concern (InteractsWithPivotTable) does not make use of parentKey at all, and instead still uses the old $this->parent->getKey() rather than $this->parent->{$this->parentKey} This references this change e4f8884 which has been superceded by 00c2c5a There also requires a change in BelongsToMany to resolve Eager Loading. --- .../Eloquent/Relations/Concerns/InteractsWithPivotTable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index b281cd2c3773..f4a9aef42656 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -293,7 +293,7 @@ protected function baseAttachRecord($id, $timed) { $record[$this->relatedPivotKey] = $id; - $record[$this->foreignPivotKey] = $this->parent->getKey(); + $record[$this->foreignPivotKey] = $this->parent->{$this->parentKey}; // If the record needs to have creation and update timestamps, we will make // them by calling the parent model's "freshTimestamp" method which will @@ -439,7 +439,7 @@ protected function newPivotQuery() call_user_func_array([$query, 'whereIn'], $arguments); } - return $query->where($this->foreignPivotKey, $this->parent->getKey()); + return $query->where($this->foreignPivotKey, $this->parent->{$this->parentKey}); } /**