Skip to content

Commit 0a2d871

Browse files
Upperfoottaylorotwell
authored andcommitted
[5.5] Completed BelongsToMany Parent Key Change (#21044)
* Resolve Eager Loading Problem with BelongsToMany This resolves the problem of the relation not making use of the parentKey for the parent Models. * 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.
1 parent 018167e commit 0a2d871

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected function addWhereConstraints()
194194
*/
195195
public function addEagerConstraints(array $models)
196196
{
197-
$this->query->whereIn($this->getQualifiedForeignPivotKeyName(), $this->getKeys($models));
197+
$this->query->whereIn($this->getQualifiedForeignPivotKeyName(), $this->getKeys($models, $this->parentKey));
198198
}
199199

200200
/**
@@ -229,7 +229,7 @@ public function match(array $models, Collection $results, $relation)
229229
// children back to their parent using the dictionary and the keys on the
230230
// the parent models. Then we will return the hydrated models back out.
231231
foreach ($models as $model) {
232-
if (isset($dictionary[$key = $model->getKey()])) {
232+
if (isset($dictionary[$key = $model->{$this->parentKey}])) {
233233
$model->setRelation(
234234
$relation, $this->related->newCollection($dictionary[$key])
235235
);

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ protected function baseAttachRecord($id, $timed)
293293
{
294294
$record[$this->relatedPivotKey] = $id;
295295

296-
$record[$this->foreignPivotKey] = $this->parent->getKey();
296+
$record[$this->foreignPivotKey] = $this->parent->{$this->parentKey};
297297

298298
// If the record needs to have creation and update timestamps, we will make
299299
// them by calling the parent model's "freshTimestamp" method which will
@@ -439,7 +439,7 @@ protected function newPivotQuery()
439439
call_user_func_array([$query, 'whereIn'], $arguments);
440440
}
441441

442-
return $query->where($this->foreignPivotKey, $this->parent->getKey());
442+
return $query->where($this->foreignPivotKey, $this->parent->{$this->parentKey});
443443
}
444444

445445
/**

0 commit comments

Comments
 (0)