From 934963408e30d1e80983116cfd4f2e5d0b0a8a72 Mon Sep 17 00:00:00 2001 From: Picasso Houessou Date: Sat, 14 Jun 2025 20:00:42 +0200 Subject: [PATCH 1/5] docs: improve parameters and filters for laravel --- laravel/filters.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/laravel/filters.md b/laravel/filters.md index d95fb7cae8b..0111ca5b6ab 100644 --- a/laravel/filters.md +++ b/laravel/filters.md @@ -44,6 +44,23 @@ 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: @@ -86,6 +103,25 @@ 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 From d62ca16e42f732cbf1b81252d881694b8aefd2cd Mon Sep 17 00:00:00 2001 From: Picasso Houessou Date: Sat, 14 Jun 2025 20:06:50 +0200 Subject: [PATCH 2/5] docs: improve parameters and filters for laravel --- laravel/filters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/filters.md b/laravel/filters.md index 0111ca5b6ab..3b2cfb9af29 100644 --- a/laravel/filters.md +++ b/laravel/filters.md @@ -116,7 +116,7 @@ use ApiPlatform\Metadata\QueryParameter; #[ApiResource] #[QueryParameter(key: 'sort[:property]', filter: OrderFilter::class, properties: ['name', 'description'])] -#[QueryParameter(key: ':property', filter: PartialSearchFilter::class, properties: ['name', 'description')] +#[QueryParameter(key: ':property', filter: PartialSearchFilter::class, properties: ['name', 'description'])] class Book extends Model { } From b4bd54253ed7d4c3a3f835a03b832fdd1d8c7424 Mon Sep 17 00:00:00 2001 From: Picasso Houessou Date: Sat, 14 Jun 2025 20:20:07 +0200 Subject: [PATCH 3/5] docs: improve parameters and filters for laravel --- laravel/filters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/filters.md b/laravel/filters.md index 3b2cfb9af29..ed2245e8aa4 100644 --- a/laravel/filters.md +++ b/laravel/filters.md @@ -103,7 +103,7 @@ 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 +### Filtering on Specific Properties Only To enable partial search filtering and sorting on specific properties like `name` and `description`: ```php From cebcba368589bdb6432505bd7fa6ce4b214d6dec Mon Sep 17 00:00:00 2001 From: Picasso Houessou Date: Sat, 14 Jun 2025 20:32:36 +0200 Subject: [PATCH 4/5] Fix lint errors --- laravel/filters.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/laravel/filters.md b/laravel/filters.md index ed2245e8aa4..1fc7cb513b6 100644 --- a/laravel/filters.md +++ b/laravel/filters.md @@ -45,6 +45,7 @@ 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 @@ -104,6 +105,7 @@ 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 From b4093f6c21c6c2c7ea14c9011a7e1e1ea5cab3e1 Mon Sep 17 00:00:00 2001 From: Picasso Houessou Date: Sat, 14 Jun 2025 21:25:03 +0200 Subject: [PATCH 5/5] fix lint errors --- laravel/filters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/filters.md b/laravel/filters.md index 1fc7cb513b6..a066e1d8e00 100644 --- a/laravel/filters.md +++ b/laravel/filters.md @@ -46,7 +46,7 @@ You can create your own filters by implementing the `ApiPlatform\Laravel\Eloquen ### Parameter for Specific Operations -To defines a parameter for only a `GetCollection` operation, you can do the following: +To defines a parameter for only a `GetCollection` operation, you can do the following: ```php // app/Models/Book.php