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]) ); 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}); } /**