Skip to content

Commit f747e40

Browse files
docs: improve parameters and filters for laravel (#2169)
* docs: improve parameters and filters for laravel * docs: improve parameters and filters for laravel * docs: improve parameters and filters for laravel * Fix lint errors * fix lint errors
1 parent 9f7c899 commit f747e40

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

laravel/filters.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ final class EqualsFilter implements FilterInterface
4444

4545
You can create your own filters by implementing the `ApiPlatform\Laravel\Eloquent\Filter\FilterInterface`. API Platform provides several eloquent filters for a RAD approach.
4646

47+
### Parameter for Specific Operations
48+
49+
To defines a parameter for only a `GetCollection` operation, you can do the following:
50+
51+
```php
52+
// app/Models/Book.php
53+
54+
use ApiPlatform\Laravel\Eloquent\Filter\EqualsFilter;
55+
use ApiPlatform\Metadata\ApiResource;
56+
use ApiPlatform\Metadata\QueryParameter;
57+
58+
#[ApiResource]
59+
#[GetCollection(parameters: ['name' => new QueryParameter(key: 'name', filter: EqualsFilter::class)])]
60+
class Book extends Model
61+
{
62+
}
63+
```
64+
4765
### Parameter Validation
4866

4967
You can add [validation rules](https://laravel.com/docs/validation) to parameters within the `constraints` attribute:
@@ -86,6 +104,26 @@ class Book extends Model
86104

87105
The documentation will output a query parameter per property that applies the `PartialSearchFilter` and also gives the ability to sort by name and ID using: `/books?name=search&order[id]=asc&order[name]=desc`.
88106

107+
### Filtering on Specific Properties Only
108+
109+
To enable partial search filtering and sorting on specific properties like `name` and `description`:
110+
111+
```php
112+
// app/Models/Book.php
113+
114+
use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
115+
use ApiPlatform\Laravel\Eloquent\Filter\OrderFilter;
116+
use ApiPlatform\Metadata\ApiResource;
117+
use ApiPlatform\Metadata\QueryParameter;
118+
119+
#[ApiResource]
120+
#[QueryParameter(key: 'sort[:property]', filter: OrderFilter::class, properties: ['name', 'description'])]
121+
#[QueryParameter(key: ':property', filter: PartialSearchFilter::class, properties: ['name', 'description'])]
122+
class Book extends Model
123+
{
124+
}
125+
```
126+
89127
## Filters
90128

91129
### Text

0 commit comments

Comments
 (0)