Skip to content

Commit 4043407

Browse files
committed
fix: alternate for where Has notHas
1 parent 5866abc commit 4043407

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ where json_unquote(json_extract(`meta\`, '$."seo"')) = 'enabled'
175175
```
176176

177177
* Relations: `filters[relationName][has]` or `filters[relationName][!has]` or `filters[relation_name][not_has]`
178-
* Relations filtering `filters[tags][has][slug]=my_slug`
178+
* Relations filtering `filters[tags][has][slug]=my_slug`
179+
* Relations `filters[tags]=true` or `filters['tags.slug']=myslug` `filters[tags.slug][!]=myslug` `filters[tags.slug][!][contains]=money`
179180

180181
## Scopes
181182

src/Http/Api/Contracts/HasParser.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,52 @@ protected function parseFiltersArray(array $filters = []): Collection
236236
$key => [ 'equals' => $value]
237237
]
238238
);
239+
240+
239241
}
240242

241243
protected function buildQuery(string $column, string $comparison, mixed $value, mixed $builder = null): void
242244
{
243245
$builder ??= $this->getBuilder();
244246

247+
$model = $builder->getModel();
248+
249+
if(str($column)->contains(".") === true){
250+
[$relation, $key] = str($column)->explode(".");
251+
if ($model->isRelation($relation)) {
252+
253+
254+
$value = match($comparison) {
255+
'is','equals','=' => [
256+
$key => $value
257+
],
258+
'!equals','not_equals','!is','not_is','!','!=','<>' => [
259+
$key => $value
260+
261+
],
262+
default => [
263+
$key => [
264+
$comparison => $value
265+
]
266+
]
267+
};
268+
269+
$comparison = match($comparison){
270+
'is','equals','=' => 'has',
271+
'!equals','not_equals','!is','not_is','!','!=','<>' => 'not_has',
272+
default => $comparison
273+
};
274+
275+
$column = $relation;
276+
}
277+
}
278+
279+
if ($model->isRelation($column)) {
280+
if ($comparison !== 'has' && $comparison !== 'not_has') {
281+
$comparison = is_array($value) || filter_var($value, FILTER_VALIDATE_BOOL) === true ? 'has' : 'not_has';
282+
}
283+
}
284+
245285
match($comparison){
246286
'ends_with', '$' => $builder->where($column, 'like', "%{$value}"),
247287
'!ends_with','not_ends_with', '!$' => $builder->where($column, 'not like', "%{$value}"),
@@ -274,6 +314,7 @@ protected function buildQuery(string $column, string $comparison, mixed $value,
274314
protected function filtersHasClause(string $relation, string $method, mixed $value, mixed $builder): void
275315
{
276316

317+
277318
$rel = Helpers::camel($relation);
278319
$relatedModel = $builder->getModel()->$rel()->getModel();
279320
$callback = is_array($value);
@@ -287,6 +328,7 @@ protected function filtersHasClause(string $relation, string $method, mixed $val
287328
)
288329
: null
289330
);
331+
290332
}
291333

292334

0 commit comments

Comments
 (0)