diff --git a/core/extensions.md b/core/extensions.md index 258d37cbac7..f1453732a87 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) +# Extensions for Doctrine, Eloquent and Elasticsearch 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)