diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index ca84d06cdbc1..c66a9c2b57eb 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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. * @@ -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. * diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 710d0a3a568c..282f6aa85fc0 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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. * @@ -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. * diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 6e0a59148954..d7e77a936f07 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -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(); diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 55b02d9b5ae8..541998de8a1c 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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(); @@ -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();