diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 566d6a888773..a49099db0a7d 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -289,7 +289,7 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey = * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ - public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $relation = null) + public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $localKey = null, $relation = null) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the @@ -307,6 +307,8 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $rela $relatedKey = $relatedKey ?: $instance->getForeignKey(); + $localKey = $localKey ?: $this->getKeyName(); + // If no table name was provided, we can guess it by concatenating the two // models using underscores in alphabetical order. The two model names // are transformed to snake case from their default CamelCase also. @@ -315,7 +317,7 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $rela } return new BelongsToMany( - $instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $relation + $instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $localKey, $relation ); } diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 1c412433c63d..4af0325dfa37 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -101,12 +101,13 @@ class BelongsToMany extends Relation * @param string $relationName * @return void */ - public function __construct(Builder $query, Model $parent, $table, $foreignKey, $relatedKey, $relationName = null) + public function __construct(Builder $query, Model $parent, $table, $foreignKey, $relatedKey, $localKey, $relationName = null) { $this->table = $table; $this->relatedKey = $relatedKey; $this->foreignKey = $foreignKey; $this->relationName = $relationName; + $this->localKey = $localKey; parent::__construct($query, $parent); } @@ -140,7 +141,7 @@ protected function performJoin($query = null) // model instance. Then we can set the "where" for the parent models. $baseTable = $this->related->getTable(); - $key = $baseTable.'.'.$this->related->getKeyName(); + $key = $baseTable.'.'.$this->localKey; $query->join($this->table, $key, '=', $this->getQualifiedRelatedKeyName()); diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php index 0976ab6ac35d..1a6cfae3bfb5 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php @@ -44,13 +44,13 @@ class MorphToMany extends BelongsToMany * @param bool $inverse * @return void */ - public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $relatedKey, $relationName = null, $inverse = false) + public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $relatedKey, $localKey, $relationName = null, $inverse = false) { $this->inverse = $inverse; $this->morphType = $name.'_type'; $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); - parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $relationName); + parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $localKey, $relationName); } /** diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php index 6c98b4e9d3dd..f1b1a77f231d 100755 --- a/tests/Database/DatabaseEloquentBelongsToManyTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -703,7 +703,7 @@ public function getRelation() { list($builder, $parent) = $this->getRelationArguments(); - return new BelongsToMany($builder, $parent, 'user_role', 'user_id', 'role_id', 'relation_name'); + return new BelongsToMany($builder, $parent, 'user_role', 'user_id', 'role_id', 'id', 'relation_name'); } public function getRelationArguments() @@ -728,7 +728,7 @@ public function getRelationArguments() $builder->shouldReceive('join')->once()->with('user_role', 'roles.id', '=', 'user_role.role_id'); $builder->shouldReceive('where')->once()->with('user_role.user_id', '=', 1); - return [$builder, $parent, 'user_role', 'user_id', 'role_id', 'relation_name']; + return [$builder, $parent, 'user_role', 'user_id', 'role_id', 'id', 'relation_name']; } } diff --git a/tests/Database/DatabaseEloquentMorphToManyTest.php b/tests/Database/DatabaseEloquentMorphToManyTest.php index 1710011a6183..6e6077b7e0f0 100644 --- a/tests/Database/DatabaseEloquentMorphToManyTest.php +++ b/tests/Database/DatabaseEloquentMorphToManyTest.php @@ -74,7 +74,7 @@ public function getRelation() { list($builder, $parent) = $this->getRelationArguments(); - return new MorphToMany($builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id'); + return new MorphToMany($builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'id'); } public function getRelationArguments() @@ -98,7 +98,7 @@ public function getRelationArguments() $builder->shouldReceive('where')->once()->with('taggables.taggable_id', '=', 1); $builder->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($parent)); - return [$builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'relation_name', false]; + return [$builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'id', 'relation_name', false]; } }