Skip to content

Commit ed00f67

Browse files
docs(core/extensions): add eloquent support (#2208)
Co-authored-by: Vincent Amstoutz <vincent.amstoutz@outlook.com>
1 parent d840a9c commit ed00f67

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

core/extensions.md

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
# Extensions for Doctrine and Elasticsearch
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)
1+
# Extensions for Doctrine, Eloquent and Elasticsearch
52

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)

0 commit comments

Comments
 (0)