diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 79f75e2dd30d..2586b2a545c3 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -987,11 +987,13 @@ public function mergeModelDefinedRelationConstraints(Builder $relation) $relationQuery = $relation->getQuery(); + $whereBindings = Arr::get($relationQuery->getRawBindings(), 'where', []); + // Here we have some relation query and the original relation. We need to copy over any // where clauses that the developer may have put in the relation definition function. // We need to remove any global scopes that the developer already removed as well. return $this->withoutGlobalScopes($removedScopes)->mergeWheres( - $relationQuery->wheres, $relationQuery->getBindings() + $relationQuery->wheres, $whereBindings ); } diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 70c9f11e1c4c..a5d58cbefbbd 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -555,6 +555,21 @@ public function testWhereHasOnNestedSelfReferencingHasManyRelationship() $this->assertEquals('Grandparent Post', $results->first()->name); } + public function testHasWithNonWhereBindings() + { + $user = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + + $user->posts()->create(['name' => 'Post 2']) + ->photos()->create(['name' => 'photo.jpg']); + + $query = EloquentTestUser::has('postWithPhotos'); + + $bindingsCount = count($query->getBindings()); + $questionMarksCount = substr_count($query->toSql(), '?'); + + $this->assertEquals($questionMarksCount, $bindingsCount); + } + public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverChunkedRequest() { $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']); @@ -971,6 +986,14 @@ public function photos() { return $this->morphMany('EloquentTestPhoto', 'imageable'); } + + public function postWithPhotos() + { + return $this->post()->join('photo', function ($join) { + $join->on('photo.imageable_id', 'post.id'); + $join->where('photo.imageable_type', 'EloquentTestPost'); + }); + } } class EloquentTestUserWithGlobalScope extends EloquentTestUser