Skip to content

Commit

Permalink
Merge pull request #29154 from ninjaparade/feature/whereNotNull-with-…
Browse files Browse the repository at this point in the history
…array-columns

[5.8] allow `whereNull` and `whereNotNull` to accept array columns argument
taylorotwell authored Jul 13, 2019
2 parents 5222f6b + af1bf7f commit 0a1a5eb
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -996,16 +996,18 @@ public function whereIntegerNotInRaw($column, $values, $boolean = 'and')
/**
* Add a "where null" clause to the query.
*
* @param string $column
* @param string|array $columns
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereNull($column, $boolean = 'and', $not = false)
public function whereNull($columns, $boolean = 'and', $not = false)
{
$type = $not ? 'NotNull' : 'Null';

$this->wheres[] = compact('type', 'column', 'boolean');
foreach (Arr::wrap($columns) as $column) {
$this->wheres[] = compact('type', 'column', 'boolean');
}

return $this;
}
26 changes: 26 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -970,6 +970,19 @@ public function testBasicWhereNulls()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testArrayWhereNulls()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNull(['id', 'expires_at']);
$this->assertEquals('select * from "users" where "id" is null and "expires_at" is null', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereNull(['id', 'expires_at']);
$this->assertEquals('select * from "users" where "id" = ? or "id" is null or "expires_at" is null', $builder->toSql());
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testBasicWhereNotNulls()
{
$builder = $this->getBuilder();
@@ -983,6 +996,19 @@ public function testBasicWhereNotNulls()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testArrayWhereNotNulls()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotNull(['id', 'expires_at']);
$this->assertEquals('select * from "users" where "id" is not null and "expires_at" is not null', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '>', 1)->orWhereNotNull(['id', 'expires_at']);
$this->assertEquals('select * from "users" where "id" > ? or "id" is not null or "expires_at" is not null', $builder->toSql());
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testGroupBys()
{
$builder = $this->getBuilder();

0 comments on commit 0a1a5eb

Please sign in to comment.