From fdb196afb7d6c4fce3d0d60e8592758d7154e78e Mon Sep 17 00:00:00 2001 From: Will Taylor-Jackson Date: Tue, 5 Feb 2019 15:45:06 +0000 Subject: [PATCH] feat: support `whereNot()` --- src/QueryBuilder.php | 18 ++++++++++++++++++ src/QueryGrammar.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 16d72c8..e1c9bbd 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -201,6 +201,24 @@ public function whereNestedDoc($column, $query, $boolean = 'and'): self return $this; } + /** + * Add a 'must not' statement to the query. + * + * @param \Illuminate\Database\Query\Builder|static $query + * @param string $boolean + * @return self + */ + public function whereNot($query, $boolean = 'and'): self + { + $type = 'Not'; + + call_user_func($query, $query = $this->newQuery()); + + $this->wheres[] = compact('query', 'type', 'boolean'); + + return $this; + } + /** * Add a prefix query * diff --git a/src/QueryGrammar.php b/src/QueryGrammar.php index cb6b23f..5e43beb 100644 --- a/src/QueryGrammar.php +++ b/src/QueryGrammar.php @@ -609,9 +609,37 @@ protected function compileWhereNestedDoc(Builder $builder, $where): array $query['nested'] = array_merge($query['nested'], array_filter($wheres)); + if (isset($where['operator']) && $where['operator'] === '!=') { + $query = [ + 'bool' => [ + 'must_not' => [ + $query + ] + ] + ]; + } + return $query; } + /** + * Compile a where not clause + * + * @param Builder $builder + * @param array $where + * @return array + */ + protected function compileWhereNot(Builder $builder, $where): array + { + return [ + 'bool' => [ + 'must_not' => [ + $this->compileWheres($where['query'])['query'] + ] + ] + ]; + } + /** * Get value for the where *