Skip to content

Commit b4f25bb

Browse files
committed
docs(laravel/filters): document new custom filter command and syntax
1 parent 95d38d8 commit b4f25bb

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

laravel/filters.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,107 @@ A few `filterContext` options are available to configure the filter:
281281
- `whitelist` properties whitelist to avoid uncontrolled data exposure (default `null` to allow all properties)
282282

283283
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
358+
{
359+
return $builder->->whereMonth($parameter->getProperty(), $values);
360+
}
361+
}
362+
```
363+
364+
We can now use it in our resources and model like other filters, for example, as follows:
365+
```php
366+
<?php
367+
368+
// app/ApiResource/Invoice.php
369+
370+
namespace App\ApiResource;
371+
372+
use ApiPlatform\Metadata\QueryParameter;
373+
use App\Filters\MonthFilter;
374+
375+
#[ApiResource]
376+
#[QueryParameter(
377+
key: 'createdAtMonth',
378+
filter: new MonthFilter(),
379+
property: 'createdAt'
380+
)]
381+
class Invoice
382+
{
383+
// ...
384+
}
385+
```
386+
And that's it! ✅ Your filter is operational. A request like `GET /invoices?createdAtMonth=7` will now correctly return
387+
the invoices from July!

0 commit comments

Comments
 (0)