Skip to content

Commit 38cb1c6

Browse files
committed
✅ include filtering on pivot
1 parent 473601f commit 38cb1c6

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,78 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
491491
['Accept' => 'application/json']
492492
);
493493

494+
$this->assertResourcePaginated(
495+
$response,
496+
[$matchingModel, $matchingModel2],
497+
new ModelResource(),
498+
[
499+
[
500+
'belongs_to_many_relation' => $matchingModel->belongsToManyRelation()
501+
->orderBy('id', 'desc')
502+
->get()
503+
->map(function ($relation) use ($pivotAccessor) {
504+
return collect($relation->only(
505+
array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor])
506+
))
507+
->pipe(function ($relation) use ($pivotAccessor) {
508+
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
509+
->only(
510+
(new ModelResource())->relation('belongsToManyRelation')->getPivotFields()
511+
);
512+
513+
return $relation;
514+
});
515+
})
516+
->toArray(),
517+
],
518+
[
519+
'belongs_to_many_relation' => [],
520+
],
521+
]
522+
);
523+
}
524+
525+
public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot(): void
526+
{
527+
$matchingModel = ModelFactory::new()
528+
->hasAttached(
529+
BelongsToManyRelationFactory::new()->count(1),
530+
['number' => 3],
531+
'belongsToManyRelation'
532+
)
533+
->create()->fresh();
534+
535+
$matchingModel2 = ModelFactory::new()
536+
->hasAttached(
537+
BelongsToManyRelationFactory::new()->count(1),
538+
['number' => 1],
539+
'belongsToManyRelation'
540+
)
541+
->create()->fresh();
542+
543+
$pivotAccessor = $matchingModel->belongsToManyRelation()->getPivotAccessor();
544+
545+
Gate::policy(Model::class, GreenPolicy::class);
546+
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);
547+
548+
$response = $this->post(
549+
'/api/models/search',
550+
[
551+
'search' => [
552+
'includes' => [
553+
[
554+
'relation' => 'belongsToManyRelation',
555+
'filters' => [
556+
['field' => 'models.pivot.number', 'operator' => '>', 'value' => 2]
557+
]
558+
],
559+
],
560+
],
561+
],
562+
['Accept' => 'application/json']
563+
);
564+
565+
494566
$this->assertResourcePaginated(
495567
$response,
496568
[$matchingModel, $matchingModel2],

tests/Support/Rest/Resources/BelongsToManyResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Lomkit\Rest\Http\Requests\RestRequest;
77
use Lomkit\Rest\Http\Resource;
88
use Lomkit\Rest\Relations\BelongsTo;
9+
use Lomkit\Rest\Relations\BelongsToMany;
910
use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation;
1011

1112
class BelongsToManyResource extends Resource
@@ -18,6 +19,7 @@ public function relations(RestRequest $request): array
1819
{
1920
return [
2021
BelongsTo::make('model', ModelQueryChangedResource::class),
22+
BelongsToMany::make('models', ModelResource::class)->withPivotFields(['number']),
2123
];
2224
}
2325

0 commit comments

Comments
 (0)