Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Completed BelongsToMany Parent Key Change #21044

Merged
merged 2 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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});
}

/**
Expand Down