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
> 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
+
28
56
You can declare a parameter on the resource class to make it available for all its operations:
29
57
30
58
```php
@@ -71,6 +99,42 @@ class Friend
71
99
}
72
100
```
73
101
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
+
74
138
### Filtering a Single Property
75
139
76
140
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