|
| 1 | +# Filtering |
| 2 | + |
| 3 | +The JSON:API specification reserves the `filter` query parameter for |
| 4 | +[filtering resources](https://jsonapi.org/format/#fetching-filtering). |
| 5 | + |
| 6 | +To define filters that can be used in this query parameter, add them to your |
| 7 | +`Listable` resource's `filters` method: |
| 8 | + |
| 9 | +```php |
| 10 | +class PostsResource extends Resource implements Listable |
| 11 | +{ |
| 12 | + // ... |
| 13 | + |
| 14 | + public function filters(): array |
| 15 | + { |
| 16 | + return [ |
| 17 | + ExampleFilter::make('example'), // [!code ++] |
| 18 | + ]; |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +::: tip Laravel Integration |
| 24 | +For Eloquent-backed resources, a number of [filters](laravel.md#filters) are |
| 25 | +provided to make it easy to implement filtering on your resource. |
| 26 | +::: |
| 27 | + |
| 28 | +## Inline Filters |
| 29 | + |
| 30 | +The easiest way to define a filter is to use the `CustomFilter` class, which |
| 31 | +accepts the name of the filter parameter and a callback to apply the filter to |
| 32 | +the query. The value received by a filter can be a string or an array, so you |
| 33 | +will need to handle both: |
| 34 | + |
| 35 | +```php |
| 36 | +use Tobyz\JsonApiServer\Schema\CustomFilter; |
| 37 | + |
| 38 | +CustomFilter::make('name', function ( |
| 39 | + $query, |
| 40 | + string|array $value, |
| 41 | + Context $context, |
| 42 | +) { |
| 43 | + $query->whereIn('name', (array) $value); |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +Now the filter can be applied like so: |
| 48 | + |
| 49 | +```http |
| 50 | +GET /posts?filter[name]=Toby |
| 51 | +GET /posts?filter[name][]=Toby&filter[name][]=Franz |
| 52 | +``` |
| 53 | + |
| 54 | +## Boolean Filters |
| 55 | + |
| 56 | +By default it is assumed that each filter applied to the query will be combined |
| 57 | +with a logical `AND`. When a resource implements |
| 58 | +`Tobyz\JsonApiServer\Resource\SupportsBooleanFilters` you can express more |
| 59 | +complex logic with `AND`, `OR`, and `NOT` groups. |
| 60 | + |
| 61 | +Boolean groups are expressed by nesting objects under the `filter` parameter. |
| 62 | +You may use either associative objects or indexed lists of clauses. Each clause |
| 63 | +can be another filter or another boolean group. |
| 64 | + |
| 65 | +```http |
| 66 | +GET /posts |
| 67 | + ?filter[and][0][status]=published |
| 68 | + &filter[and][1][or][0][views][gt]=100 |
| 69 | + &filter[and][1][or][1][not][status]=archived |
| 70 | +``` |
| 71 | + |
| 72 | +In this request every result must be published, and it must also either have |
| 73 | +more than 100 views or it is not archived. |
| 74 | + |
| 75 | +```http |
| 76 | +GET /posts |
| 77 | + ?filter[or][0][status]=draft |
| 78 | + &filter[or][1][status]=published |
| 79 | + &filter[or][1][not][comments]=0 |
| 80 | +``` |
| 81 | + |
| 82 | +This request returns drafts, or posts that are published and have comments. The |
| 83 | +second example also shows that in certain cases you can omit `[and]` groups and |
| 84 | +numeric indices; sibling filters at the same level default to `AND` behaviour. |
| 85 | + |
| 86 | +## Writing Filters |
| 87 | + |
| 88 | +To create your own filter class, extend the `Tobyz\JsonApiServer\Schema\Filter` |
| 89 | +class and implement the `apply` method: |
| 90 | + |
| 91 | +```php |
| 92 | +use Tobyz\JsonApiServer\Context; |
| 93 | +use Tobyz\JsonApiServer\Schema\Filter; |
| 94 | + |
| 95 | +class WhereIn extends Filter |
| 96 | +{ |
| 97 | + public function apply( |
| 98 | + object $query, |
| 99 | + string|array $value, |
| 100 | + Context $context, |
| 101 | + ): void { |
| 102 | + $query->whereIn($this->name, $value); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Visibility |
| 108 | + |
| 109 | +If you want to restrict the ability to use a filter, use the `visible` or |
| 110 | +`hidden` method, passing a closure that returns a boolean value: |
| 111 | + |
| 112 | +```php |
| 113 | +WhereIn::make('example')->visible( |
| 114 | + fn(Context $context) => $context->request->getAttribute('isAdmin'), |
| 115 | +); |
| 116 | +``` |
0 commit comments