Skip to content

Commit 0ca8605

Browse files
phpsacraigAtCD
andauthored
Feature/filter null not null (#91)
* query builder not looks for null no null * Null Filter documentation * Apply fixes from StyleCI (#92) Co-authored-by: Craig Smith <[email protected]> Co-authored-by: Craig Smith <[email protected]> Co-authored-by: Craig Smith <[email protected]>
1 parent 639787c commit 0ca8605

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ For the get command you can filter by using the following url patterns
112112
You can pass to the filters an array of values
113113
ie: `filter[user_id]=1||2||||4||7` or `filter[user_id!]=55||33`
114114

115+
# Null / Not Null (introduced 1.23.0)
116+
117+
If you need to filter on whether a field is null or not null, you can use the filter param as of version 1.23.0 EG: `filter[age]=NULL` or `filter[age!]=NULL`. Note that NULL must be uppercase.
118+
119+
**Older versions**
120+
Add a scope to your model: eg
121+
122+
```php
123+
124+
public function scopeAgeNull(Builder $builder, $isNull = true){
125+
$isNull ? $builder->whereNull('age') : $builder->whereNotNull('age');
126+
}
127+
```
128+
129+
Add to your allowedScopes and can then be called in url as `?ageNull=1` for where null and `?ageNull=0` for where age not null
130+
115131
## Scopes
116132

117133
In addition to filtering, you can use Laravel's Eloquent [Query Scopes](https://laravel.com/docs/6.x/eloquent#local-scopes) to do more complex searches or filters.

src/Contracts/Parser.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected function parseFilterParams(): void
193193
} elseif (! in_array($whr['key'], $tableColumns)) {
194194
continue;
195195
}
196-
$this->setQueryBuilderWhereStatement($this->repository, $table . '.' . $whr['key'], $whr);
196+
$this->setQueryBuilderWhereStatement($this->repository, $table.'.'.$whr['key'], $whr);
197197
}
198198
}
199199

@@ -239,16 +239,20 @@ protected function setQueryBuilderWhereStatement($query, $key, $where): void
239239
if (! empty($where['values'])) {
240240
$query->whereIn($key, $where['values']);
241241
}
242-
243-
return;
242+
break;
244243
case 'NotIn':
245244
if (! empty($where['values'])) {
246245
$query->whereNotIn($key, $where['values']);
247246
}
248-
249-
return;
247+
break;
250248
case 'Basic':
251-
$query->where($key, $where['operator'], $where['value']);
249+
if ($where['value'] !== 'NULL') {
250+
$query->where($key, $where['operator'], $where['value']);
251+
252+
return;
253+
}
254+
255+
$where['operator'] === '=' ? $query->whereNull($key) : $query->whereNotNull($key);
252256
}
253257
}
254258

0 commit comments

Comments
 (0)