Skip to content

Commit

Permalink
in/not in as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jerguslejko committed Apr 11, 2019
1 parent 68981ba commit bc32756
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit bc32756

Please sign in to comment.