Skip to content

Commit 26c3a86

Browse files
josip-miloticjmiloticStyleCIBot
authored
Doesnt have relations #minor (#38)
* Added search for models that don't have specified relations * Apply fixes from StyleCI Co-authored-by: jmilotic <josip.milotic@asseco-see.hr> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent e536c47 commit 26c3a86

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ JSON parameters (keys):
5757
- `offset` - will return a subset of results starting from a point given. This parameter **MUST**
5858
be used together with ``limit`` parameter.
5959
- `count` - will return record count.
60-
- `soft_deleted` - will include soft deleted models in search results
60+
- `soft_deleted` - will include soft deleted models in search results.
61+
- `doesnt_have_relations` - will only return entries that don't have any of the specified relations.
6162

6263
### Search
6364

@@ -357,6 +358,24 @@ with ``soft_deleted``:
357358
}
358359
```
359360

361+
### Doesn't have relations
362+
363+
In case you want to only find entries without a specified relation, you can do so with ``doesnt_have_relations`` key:
364+
365+
```
366+
{
367+
"doesnt_have_relations": "containers"
368+
}
369+
```
370+
371+
If you want to specify multiple relations, you can do so in the following way:
372+
373+
```
374+
{
375+
"doesnt_have_relations": ["containers", "addresses"]
376+
}
377+
```
378+
360379
## Top level logical operators
361380

362381
Additionally, it is possible to group search clauses by top-level logical operator.

config/asseco-json-query-builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Asseco\JsonQueryBuilder\RequestParameters\CountParameter;
4+
use Asseco\JsonQueryBuilder\RequestParameters\DoesntHaveRelationsParameter;
45
use Asseco\JsonQueryBuilder\RequestParameters\GroupByParameter;
56
use Asseco\JsonQueryBuilder\RequestParameters\LimitParameter;
67
use Asseco\JsonQueryBuilder\RequestParameters\OffsetParameter;
@@ -34,6 +35,7 @@
3435
CountParameter::class,
3536
GroupByParameter::class,
3637
SoftDeletedParameter::class,
38+
DoesntHaveRelationsParameter::class,
3739
],
3840

3941
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Asseco\JsonQueryBuilder\RequestParameters;
6+
7+
use Asseco\JsonQueryBuilder\Exceptions\JsonQueryBuilderException;
8+
use Illuminate\Support\Str;
9+
10+
class DoesntHaveRelationsParameter extends AbstractParameter
11+
{
12+
public static function getParameterName(): string
13+
{
14+
return 'doesnt_have_relations';
15+
}
16+
17+
protected function appendQuery(): void
18+
{
19+
foreach ($this->arguments as $argument) {
20+
if (is_string($argument)) {
21+
$this->builder->doesntHave(Str::camel($argument));
22+
continue;
23+
}
24+
25+
throw new JsonQueryBuilderException('Wrong relation parameters provided.');
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)