diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index b133e000eb56..a35ab7c54d36 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -138,7 +138,7 @@ public function addEagerConstraints(array $models) { $table = $this->parent->getTable(); - $this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($models)); + $this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($models, $this->localKey)); } /** diff --git a/tests/Database/DatabaseEloquentHasManyThroughTest.php b/tests/Database/DatabaseEloquentHasManyThroughTest.php index 6c42c4d2de76..7ea4d7bcb67c 100644 --- a/tests/Database/DatabaseEloquentHasManyThroughTest.php +++ b/tests/Database/DatabaseEloquentHasManyThroughTest.php @@ -36,6 +36,36 @@ public function testEagerConstraintsAreProperlyAdded() $relation->addEagerConstraints([$model1, $model2]); } + public function testEagerConstraintsAreProperlyAddedWithCustomKey() + { + $builder = m::mock('Illuminate\Database\Eloquent\Builder'); + $builder->shouldReceive('join')->once()->with('users', 'users.id', '=', 'posts.user_id'); + $builder->shouldReceive('where')->with('users.country_id', '=', 1); + + $country = m::mock('Illuminate\Database\Eloquent\Model'); + $country->shouldReceive('getKeyName')->andReturn('id'); + $country->shouldReceive('offsetGet')->andReturn(1); + $country->shouldReceive('getForeignKey')->andReturn('country_id'); + + $user = m::mock('Illuminate\Database\Eloquent\Model'); + $user->shouldReceive('getTable')->andReturn('users'); + $user->shouldReceive('getQualifiedKeyName')->andReturn('users.id'); + $post = m::mock('Illuminate\Database\Eloquent\Model'); + $post->shouldReceive('getTable')->andReturn('posts'); + + $builder->shouldReceive('getModel')->andReturn($post); + + $relation = new HasManyThrough($builder, $country, $user, 'country_id', 'user_id', 'not_id'); + $relation->getQuery()->shouldReceive('whereIn')->once()->with('users.country_id', [3, 4]); + $model1 = new EloquentHasManyThroughModelStub; + $model1->id = 1; + $model1->not_id = 3; + $model2 = new EloquentHasManyThroughModelStub; + $model2->id = 2; + $model2->not_id = 4; + $relation->addEagerConstraints([$model1, $model2]); + } + public function testModelsAreProperlyMatchedToParents() { $relation = $this->getRelation();