Skip to content

Commit

Permalink
Add whereNot() and orWhereNot() to query builders
Browse files Browse the repository at this point in the history
  • Loading branch information
shadoWalker89 committed Mar 9, 2021
1 parent 0798479 commit 7d104fe
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
return $this;
}

/**
* Add a basic "where not" clause to the query.
*
* @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function whereNot($column, $operator = null, $value = null)
{
return $this->where($column, $operator, $value, 'and not');
}

/**
* Add a basic where clause to the query, and return the first result.
*
Expand Down Expand Up @@ -297,6 +310,19 @@ public function orWhere($column, $operator = null, $value = null)
return $this->where($column, $operator, $value, 'or');
}

/**
* Add an "or where not" clause to the query.
*
* @param \Closure|array|string|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function orWhereNot($column, $operator = null, $value = null)
{
return $this->where($column, $operator, $value, 'or not');
}

/**
* Add an "order by" clause for a timestamp to the query.
*
Expand Down
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,19 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
return $this;
}

/**
* Add a basic "where not" clause to the query.
*
* @param \Closure|string|array $column
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function whereNot($column, $operator = null, $value = null)
{
return $this->where($column, $operator, $value, 'and not');
}

/**
* Add an array of where clauses to the query.
*
Expand Down Expand Up @@ -844,6 +857,19 @@ public function orWhere($column, $operator = null, $value = null)
return $this->where($column, $operator, $value, 'or');
}

/**
* Add an "or where not" clause to the query.
*
* @param \Closure|string|array $column
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function orWhereNot($column, $operator = null, $value = null)
{
return $this->where($column, $operator, $value, 'or not');
}

/**
* Add a "where" clause comparing two columns to the query.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,24 @@ public function testSimpleWhere()
$this->assertEquals($result, $builder);
}

public function testSimpleWhereNot()
{
$model = new EloquentBuilderTestStub();
$this->mockConnectionForModel($model, 'SQLite');
$query = $model->newQuery()->whereNot("name", "foo")->whereNot("name", "<>", "bar");
$this->assertEquals('select * from "table" where not "name" = ? and not "name" <> ?', $query->toSql());
$this->assertEquals(["foo", "bar"], $query->getBindings());
}

public function testSimpleOrWhereNot()
{
$model = new EloquentBuilderTestStub();
$this->mockConnectionForModel($model, 'SQLite');
$query = $model->newQuery()->orWhereNot("name", "foo")->orWhereNot("name", "<>", "bar");
$this->assertEquals('select * from "table" where not "name" = ? or not "name" <> ?', $query->toSql());
$this->assertEquals(["foo", "bar"], $query->getBindings());
}

public function testPostgresOperatorsWhere()
{
$builder = $this->getBuilder();
Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ public function testBasicWheres()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testBasicWhereNot()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNot('name', "foo")->whereNot('name', '<>', "bar");
$this->assertSame('select * from "users" where not "name" = ? and not "name" <> ?', $builder->toSql());
$this->assertEquals(["foo", "bar"], $builder->getBindings());
}

public function testWheresWithArrayValue()
{
$builder = $this->getBuilder();
Expand Down Expand Up @@ -701,6 +709,14 @@ public function testBasicOrWheres()
$this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
}

public function testBasicOrWhereNot()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->orWhereNot('name', "foo")->orWhereNot('name', '<>', "bar");
$this->assertSame('select * from "users" where not "name" = ? or not "name" <> ?', $builder->toSql());
$this->assertEquals(["foo", "bar"], $builder->getBindings());
}

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

0 comments on commit 7d104fe

Please sign in to comment.