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.4] Fix JoinClause's whereIn when using a subquery #20453

Merged
merged 3 commits into from
Aug 14, 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
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Database\Concerns\BuildsQueries;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

class Builder
{
Expand Down Expand Up @@ -725,10 +726,14 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
{
$type = $not ? 'NotIn' : 'In';

if ($values instanceof EloquentBuilder) {
$values = $values->getQuery();
}

// If the value is a query builder instance we will assume the developer wants to
// look for any values that exists within this given query. So we will add the
// query accordingly so that this query is properly executed when it is run.
if ($values instanceof static) {
if ($values instanceof self) {
return $this->whereInExistingQuery(
$column, $values, $boolean, $not
);
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,27 @@ public function testJoinWhereIn()
$this->assertEquals([48, 'baz', null], $builder->getBindings());
}

public function testJoinWhereInSubquery()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->join('contacts', function ($j) {
$q = $this->getBuilder();
$q->select('name')->from('contacts')->where('name', 'baz');
$j->on('users.id', '=', 'contacts.id')->whereIn('contacts.name', $q);
});
$this->assertEquals('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."name" in (select "name" from "contacts" where "name" = ?)', $builder->toSql());
$this->assertEquals(['baz'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->join('contacts', function ($j) {
$q = $this->getBuilder();
$q->select('name')->from('contacts')->where('name', 'baz');
$j->on('users.id', '=', 'contacts.id')->orWhereIn('contacts.name', $q);
});
$this->assertEquals('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "contacts"."name" in (select "name" from "contacts" where "name" = ?)', $builder->toSql());
$this->assertEquals(['baz'], $builder->getBindings());
}

public function testJoinWhereNotIn()
{
$builder = $this->getBuilder();
Expand Down