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.5] Allow where and dynamicWhere to handle arrays #20470

Closed
Closed
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
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
$value = new Expression($value ? 'true' : 'false');
}

if (is_array($value) || $value instanceof Arrayable) {
return $this->whereIn($column, $value, $boolean, $this->notEqualOperator($operator));
}

// Now that we are working with just a simple query we can put the elements
// in our array and add the query binding to our array of bindings that
// will be bound to each SQL statements when it is finally executed.
Expand Down Expand Up @@ -631,6 +635,17 @@ protected function invalidOperator($operator)
! in_array(strtolower($operator), $this->grammar->getOperators(), true);
}

/**
* Determine if the given operator is used to determine not equals.
*
* @param string $operator
* @return bool
*/
protected function notEqualOperator($operator)
{
return in_array($operator, ['<>', '!=']);
}

/**
* Add an "or where" clause to the query.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Collection;
use Illuminate\Database\Query\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Database\Query\Expression as Raw;
Expand Down Expand Up @@ -267,6 +268,43 @@ public function testBasicWheres()
$this->assertEquals([0 => 1], $builder->getBindings());
}

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

public function testArrayNotWheres()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '<>', [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, 2, 3]);
$this->assertEquals('select * from "users" where "id" not in (?, ?, ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
}

public function testCollectionWheres()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', Collection::make([1, 2, 3]));
$this->assertEquals('select * from "users" where "id" in (?, ?, ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
}

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

public function testMySqlWrappingProtectsQuotationMarks()
{
$builder = $this->getMySqlBuilder();
Expand Down