From b8dc88c9a6457fe51eb61769f4636b66ee391c4e Mon Sep 17 00:00:00 2001 From: Takashi Kanemoto Date: Thu, 18 Sep 2025 01:17:36 +0900 Subject: [PATCH 1/2] docs(extensions): add Eloquent support --- core/extensions.md | 58 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/core/extensions.md b/core/extensions.md index 258d37cbac7..dad1ea9d7e3 100644 --- a/core/extensions.md +++ b/core/extensions.md @@ -1,11 +1,8 @@ # Extensions for Doctrine and Elasticsearch -> [!WARNING] -> This is not yet available with [Eloquent](https://laravel.com/docs/eloquent), you're welcome to contribute [on GitHub](https://github.com/api-platform/core) - API Platform provides a system to extend queries on items and collections. -Extensions are specific to Doctrine and Elasticsearch-PHP, and therefore, the Doctrine ORM / MongoDB ODM support or the Elasticsearch +Extensions are specific to Doctrine, Eloquent and Elasticsearch-PHP, and therefore, the Doctrine ORM / MongoDB ODM support, Eloquent support or the Elasticsearch reading support must be enabled to use this feature. If you use custom providers it's up to you to implement your own extension system or not. @@ -160,6 +157,59 @@ The tags are `api_platform.doctrine_mongodb.odm.aggregation_extension.item` and The custom extensions receive the [aggregation builder](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/current/reference/aggregation-builder.html), used to execute [complex operations on data](https://docs.mongodb.com/manual/aggregation/). +## Custom Eloquent Extension + +Custom extensions must implement `ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface` and be tagged with the interface name, so they will be executed both when querying for a collection of items and when querying for an item. + +```php +getModel() instanceof Offer) { + return $builder; + } + + if (!$builder->getModel() instanceof Offer || !($user = Auth::user()) instanceof User || $user->is_admin) { + return $builder; + } + + return $builder->where('user_id', $user->id); + } +} +``` + +```php +app->tag([OfferExtension::class], QueryExtensionInterface::class); + } +} +``` + ## Custom Elasticsearch Extension Currently only extensions querying for a collection of items through a [search request](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html) From 7a4ce09d5a8e5b672953a902d4808af3e091acbe Mon Sep 17 00:00:00 2001 From: Takashi Kanemoto <4360663+ttskch@users.noreply.github.com> Date: Fri, 19 Sep 2025 19:24:36 +0900 Subject: [PATCH 2/2] Update core/extensions.md Co-authored-by: Vincent Amstoutz --- core/extensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/extensions.md b/core/extensions.md index dad1ea9d7e3..f1453732a87 100644 --- a/core/extensions.md +++ b/core/extensions.md @@ -1,4 +1,4 @@ -# Extensions for Doctrine and Elasticsearch +# Extensions for Doctrine, Eloquent and Elasticsearch API Platform provides a system to extend queries on items and collections.