Skip to content

Commit ff2ccb4

Browse files
committed
update docs
1 parent 6a249e8 commit ff2ccb4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

core/filters.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ The recommended way to define parameters is by using Parameter attributes direct
2525
* `ApiPlatform\Metadata\QueryParameter`: For URL query parameters (e.g., `?name=value`).
2626
* `ApiPlatform\Metadata\HeaderParameter`: For HTTP headers (e.g., `Custom-Header: value`).
2727

28+
### List of Available Filters
29+
30+
When defining a `QueryParameter`, you must specify the filtering logic using the `filter` option.
31+
32+
Here is a list of available filters you can use. You can pass the filter class name (recommended) or a new instance:
33+
34+
* **`DateFilter`**: For filtering by date intervals (e.g., `?createdAt[after]=...`).
35+
* Usage: `new QueryParameter(filter: DateFilter::class)`
36+
* **`ExactFilter`**: For exact value matching.
37+
* Usage: `new QueryParameter(filter: ExactFilter::class)`
38+
* **`PartialSearchFilter`**: For partial string matching (SQL `LIKE %...%`).
39+
* Usage: `new QueryParameter(filter: PartialSearchFilter::class)`
40+
* **`IriFilter`**: For filtering by IRIs (e.g., relations).
41+
* Usage: `new QueryParameter(filter: IriFilter::class)`
42+
* **`BooleanFilter`**: For boolean field filtering.
43+
* Usage: `new QueryParameter(filter: BooleanFilter::class)`
44+
* **`NumericFilter`**: For numeric field filtering.
45+
* Usage: `new QueryParameter(filter: NumericFilter::class)`
46+
* **`RangeFilter`**: For range-based filtering (e.g., prices between X and Y).
47+
* Usage: `new QueryParameter(filter: RangeFilter::class)`
48+
* **`ExistsFilter`**: For checking existence of nullable values.
49+
* Usage: `new QueryParameter(filter: ExistsFilter::class)`
50+
* **`OrderFilter`**: For sorting results.
51+
* Usage: `new QueryParameter(filter: OrderFilter::class)`
52+
53+
> [!TIP]
54+
> Always check the specific documentation for your persistence layer (Doctrine ORM, MongoDB ODM, Laravel Eloquent) to see the exact namespace and available options for these filters.
55+
2856
You can declare a parameter on the resource class to make it available for all its operations:
2957

3058
```php
@@ -71,6 +99,42 @@ class Friend
7199
}
72100
```
73101

102+
### Using Filters with DateTime Properties
103+
104+
When working with `DateTime` or `DateTimeImmutable` properties, the system might default to exact matching. To enable date ranges (e.g., `after`, `before`), you must explicitly use the `DateFilter`:
105+
106+
```php
107+
<?php
108+
// api/src/Entity/Event.php
109+
namespace App\Entity;
110+
111+
use ApiPlatform\Metadata\ApiResource;
112+
use ApiPlatform\Metadata\GetCollection;
113+
use ApiPlatform\Metadata\QueryParameter;
114+
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
115+
116+
#[ApiResource(operations: [
117+
new GetCollection(
118+
parameters: [
119+
'date[:property]' => new QueryParameter(
120+
// Use the class string to leverage the service container (recommended)
121+
filter: DateFilter::class,
122+
properties: ['startDate', 'endDate']
123+
)
124+
]
125+
)
126+
])]
127+
class Event
128+
{
129+
// ...
130+
}
131+
```
132+
133+
This configuration allows clients to filter events by date ranges using queries like:
134+
135+
* `/events?date[startDate][after]=2023-01-01`
136+
* `/events?date[endDate][before]=2023-12-31`
137+
74138
### Filtering a Single Property
75139

76140
Most of the time, a parameter maps directly to a property on your resource. For example, a `?name=Frodo` query parameter would filter for resources where the `name` property is "Frodo". This behavior is often handled by built-in or custom filters that you link to the parameter.

0 commit comments

Comments
 (0)