|
1 | 1 | # Extensions for Doctrine and Elasticsearch
|
2 | 2 |
|
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 |
| -
|
6 | 3 | API Platform provides a system to extend queries on items and collections.
|
7 | 4 |
|
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 |
9 | 6 | reading support must be enabled to use this feature. If you use custom providers it's up to you to implement your own
|
10 | 7 | extension system or not.
|
11 | 8 |
|
@@ -160,6 +157,59 @@ The tags are `api_platform.doctrine_mongodb.odm.aggregation_extension.item` and
|
160 | 157 | The custom extensions receive the [aggregation builder](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/current/reference/aggregation-builder.html),
|
161 | 158 | used to execute [complex operations on data](https://docs.mongodb.com/manual/aggregation/).
|
162 | 159 |
|
| 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 | + |
163 | 213 | ## Custom Elasticsearch Extension
|
164 | 214 |
|
165 | 215 | 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)
|
|
0 commit comments