You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: laravel/filters.md
+104Lines changed: 104 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -281,3 +281,107 @@ A few `filterContext` options are available to configure the filter:
281
281
-`whitelist` properties whitelist to avoid uncontrolled data exposure (default `null` to allow all properties)
282
282
283
283
Given that the collection endpoint is `/books`, you can filter the serialization properties with the following query: `/books?properties[]=title&properties[]=author`.
284
+
285
+
### Creating Custom Filters (API Platform >= 4.2)
286
+
287
+
#### Generating the Filter Skeleton
288
+
289
+
To get started, API Platform includes a very handy make command to generate the basic structure of an Laravel Eloquent filter:
290
+
291
+
```console
292
+
bin/console make:filter
293
+
```
294
+
295
+
Then, provide the name of your filter, for example `MonthFilter`, or pass it directly as an argument:
296
+
297
+
```console
298
+
make:filter MyCustomFilter
299
+
```
300
+
301
+
You will get a file at `app/Filter/MonthFilter.php` with the following content:
302
+
303
+
```php
304
+
<?php
305
+
// app/Filter/MonthFilter.php
306
+
<?php
307
+
308
+
declare(strict_types=1);
309
+
310
+
namespace App\Filter;
311
+
312
+
use ApiPlatform\Metadata\Parameter;
313
+
use Illuminate\Database\Eloquent\Builder;
314
+
use Illuminate\Database\Eloquent\Model;
315
+
316
+
final class MonthFilter implements FilterInterface
317
+
{
318
+
/**
319
+
* @param Builder<Model> $builder
320
+
* @param array<string,mixed> $context
321
+
*/
322
+
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder
323
+
{
324
+
// TODO: make your awesome query using the $builder
325
+
// return $builder->
326
+
}
327
+
}
328
+
```
329
+
330
+
#### Implementing a Custom Filter
331
+
332
+
Let's create a concrete filter that allows fetching entities based on the month of a date field (e.g., `createdAt`).
333
+
334
+
The goal is to be able to call a URL like `GET /invoices?createdAtMonth=7` to get all invoices created in July.
335
+
336
+
Here is the complete and corrected code for the filter:
337
+
338
+
```php
339
+
<?php
340
+
// app/Filter/MonthFilter.php
341
+
<?php
342
+
343
+
declare(strict_types=1);
344
+
345
+
namespace App\Filter;
346
+
347
+
use ApiPlatform\Metadata\Parameter;
348
+
use Illuminate\Database\Eloquent\Builder;
349
+
use Illuminate\Database\Eloquent\Model;
350
+
351
+
final class MonthFilter implements FilterInterface
352
+
{
353
+
/**
354
+
* @param Builder<Model> $builder
355
+
* @param array<string,mixed> $context
356
+
*/
357
+
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder
0 commit comments