Skip to content

Commit b8bf3fd

Browse files
Add documentation for the operator filter (#974)
--- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/spatie/laravel-query-builder?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 474b341 commit b8bf3fd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/features/filtering.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ $users = QueryBuilder::for(User::class)
8686
// $users will contain all admin users with id 1, 2, 3, 4 or 5
8787
```
8888

89+
## Operator filters
90+
91+
Operator filters allow you to filter results based on different operators such as EQUAL, NOT_EQUAL, GREATER_THAN, LESS_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN_OR_EQUAL, and DYNAMIC. You can use the `AllowedFilter::operator` method to create operator filters.
92+
93+
```php
94+
use Spatie\QueryBuilder\AllowedFilter;
95+
use Spatie\QueryBuilder\Enums\FilterOperator;
96+
97+
// GET /users?filter[salary]=>3000
98+
$users = QueryBuilder::for(User::class)
99+
->allowedFilters([
100+
AllowedFilter::operator('salary', FilterOperator::GREATER_THAN),
101+
])
102+
->get();
103+
104+
// $users will contain all users with a salary greater than 3000
105+
```
106+
107+
You can also use dynamic operator filters, which allow you to specify the operator in the filter value:
108+
109+
```php
110+
use Spatie\QueryBuilder\AllowedFilter;
111+
use Spatie\QueryBuilder\Enums\FilterOperator;
112+
113+
// GET /users?filter[salary]=>3000
114+
$users = QueryBuilder::for(User::class)
115+
->allowedFilters([
116+
AllowedFilter::operator('salary', FilterOperator::DYNAMIC),
117+
])
118+
->get();
119+
120+
// $users will contain all users with a salary greater than 3000
121+
```
122+
89123
## Exact or partial filters for related properties
90124

91125
You can also add filters for a relationship property using the dot-notation: `AllowedFilter::exact('posts.title')`. This works for exact and partial filters. Under the hood we'll add a `whereHas` statement for the `posts` that filters for the given `title` property as well.

0 commit comments

Comments
 (0)