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

[8.x] Add whereNot() and orWhereNot() to query builders #36522

Closed
wants to merge 1 commit into from
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
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