Skip to content

Commit

Permalink
Merge pull request #84 from designmynight/where-not
Browse files Browse the repository at this point in the history
feat: support `whereNot()`
  • Loading branch information
Will Taylor-Jackson authored Feb 5, 2019
2 parents f82324d + fdb196a commit 05ce320
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
28 changes: 28 additions & 0 deletions src/QueryGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 05ce320

Please sign in to comment.