Skip to content

docs: improve parameters and filters for laravel #2169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 4.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions laravel/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ final class EqualsFilter implements FilterInterface

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

### Parameter for Specific Operations

To defines a parameter for only a `GetCollection` operation, you can do the following:

```php
// app/Models/Book.php

use ApiPlatform\Laravel\Eloquent\Filter\EqualsFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\QueryParameter;

#[ApiResource]
#[GetCollection(parameters: ['name' => new QueryParameter(key: 'name', filter: EqualsFilter::class)])]
class Book extends Model
{
}
```

### Parameter Validation

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

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`.

### Filtering on Specific Properties Only

To enable partial search filtering and sorting on specific properties like `name` and `description`:

```php
// app/Models/Book.php

use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
use ApiPlatform\Laravel\Eloquent\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\QueryParameter;

#[ApiResource]
#[QueryParameter(key: 'sort[:property]', filter: OrderFilter::class, properties: ['name', 'description'])]
#[QueryParameter(key: ':property', filter: PartialSearchFilter::class, properties: ['name', 'description'])]
class Book extends Model
{
}
```

## Filters

### Text
Expand Down