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.8] in/not in operators #28192

Merged
merged 1 commit into from
Apr 12, 2019
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
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,17 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
return $this->whereNested($column, $boolean);
}

// If the operator is a literal string 'in' or 'not in', we will assume
// the developer wants to use the corresponding 'in' or 'not in' SQL
// operators. We will simply proxy to the query builder methods.
if ($operator == 'in') {
return $this->whereIn($column, $value, $boolean);
}

if ($operator == 'not in') {
return $this->whereNotIn($column, $value, $boolean);
}

// If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and
// we will set the operators to '=' and set the values appropriately.
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,11 @@ public function testBasicWhereIns()
$this->assertEquals('select * from "users" where "id" in (?, ?, ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 'in', [1, 2, 3]);
$this->assertEquals('select * from "users" where "id" in (?, ?, ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereIn('id', [1, 2, 3]);
$this->assertEquals('select * from "users" where "id" = ? or "id" in (?, ?, ?)', $builder->toSql());
Expand All @@ -657,6 +662,11 @@ public function testBasicWhereNotIns()
$this->assertEquals('select * from "users" where "id" not in (?, ?, ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 'not in', [1, 2, 3]);
$this->assertEquals('select * from "users" where "id" not in (?, ?, ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereNotIn('id', [1, 2, 3]);
$this->assertEquals('select * from "users" where "id" = ? or "id" not in (?, ?, ?)', $builder->toSql());
Expand Down