Skip to content

Commit 7d0b161

Browse files
committed
docs(extensions): add Eloquent support
1 parent d423095 commit 7d0b161

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

core/extensions.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# Extensions for Doctrine and Elasticsearch
22

3-
> [!WARNING]
4-
> 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)
5-
63
API Platform provides a system to extend queries on items and collections.
74

8-
Extensions are specific to Doctrine and Elasticsearch-PHP, and therefore, the Doctrine ORM / MongoDB ODM support or the Elasticsearch
5+
Extensions are specific to Doctrine, Eloquent and Elasticsearch-PHP, and therefore, the Doctrine ORM / MongoDB ODM support, Eloquent support or the Elasticsearch
96
reading support must be enabled to use this feature. If you use custom providers it's up to you to implement your own
107
extension system or not.
118

@@ -160,6 +157,59 @@ The tags are `api_platform.doctrine_mongodb.odm.aggregation_extension.item` and
160157
The custom extensions receive the [aggregation builder](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/current/reference/aggregation-builder.html),
161158
used to execute [complex operations on data](https://docs.mongodb.com/manual/aggregation/).
162159

160+
## Custom Eloquent Extension
161+
162+
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.
163+
164+
```php
165+
<?php
166+
// api/app/Eloquent/OfferExtension.php
167+
168+
namespace App\Eloquent;
169+
170+
use ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface;
171+
use ApiPlatform\Metadata\Operation;
172+
use App\Models\Offer;
173+
use App\Models\User;
174+
use Illuminate\Database\Eloquent\Builder;
175+
use Illuminate\Support\Facades\Auth;
176+
177+
final readonly class OfferExtension implements QueryExtensionInterface
178+
{
179+
public function apply(Builder $builder, array $uriVariables, Operation $operation, $context = []): Builder
180+
{
181+
if (!$builder->getModel() instanceof Offer) {
182+
return $builder;
183+
}
184+
185+
if (!$builder->getModel() instanceof Offer || !($user = Auth::user()) instanceof User || $user->is_admin) {
186+
return $builder;
187+
}
188+
189+
return $builder->where('user_id', $user->id);
190+
}
191+
}
192+
```
193+
194+
```php
195+
<?php
196+
// api/app/Providers/AppServiceProvider.php
197+
198+
namespace App\Providers;
199+
200+
use ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface;
201+
use App\Eloquent\OfferExtension;
202+
use Illuminate\Support\ServiceProvider;
203+
204+
class AppServiceProvider extends ServiceProvider
205+
{
206+
public function register(): void
207+
{
208+
$this->app->tag([OfferExtension::class], QueryExtensionInterface::class);
209+
}
210+
}
211+
```
212+
163213
## Custom Elasticsearch Extension
164214

165215
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)

symfony/security.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ resources:
337337

338338
## Filtering Collection According to the Current User Permissions
339339

340-
Filtering collections according to the role or permissions of the current user must be done directly at [the state provider](../core/state-providers.md) level. For instance, when using the built-in adapters for Doctrine ORM, MongoDB and ElasticSearch, removing entries from a collection should be done using [extensions](../core/extensions.md).
340+
Filtering collections according to the role or permissions of the current user must be done directly at [the state provider](../core/state-providers.md) level. For instance, when using the built-in adapters for Doctrine ORM, MongoDB, Eloquent and ElasticSearch, removing entries from a collection should be done using [extensions](../core/extensions.md).
341341
Extensions allow to customize the generated DQL/Mongo/Elastic/... query used to retrieve the collection (e.g. add `WHERE` clauses depending of the currently connected user) instead of using access control expressions.
342342
As extensions are services, you can [inject the Symfony `Security` class](https://symfony.com/doc/current/security.html#b-fetching-the-user-from-a-service) into them to access to current user's roles and permissions.
343343

0 commit comments

Comments
 (0)