Skip to content

Commit 35d0021

Browse files
authored
Merge pull request #144 from Lomkit/feature/include-filtering-on-pivot
✅ include filtering on pivot
2 parents 473601f + 6481540 commit 35d0021

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php

+142
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,146 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
521521
]
522522
);
523523
}
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+
$this->assertResourcePaginated(
566+
$response,
567+
[$matchingModel, $matchingModel2],
568+
new ModelResource(),
569+
[
570+
[
571+
'belongs_to_many_relation' => $matchingModel->belongsToManyRelation()
572+
->orderBy('id', 'desc')
573+
->get()
574+
->map(function ($relation) use ($pivotAccessor) {
575+
return collect($relation->only(
576+
array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor])
577+
))
578+
->pipe(function ($relation) use ($pivotAccessor) {
579+
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
580+
->only(
581+
(new ModelResource())->relation('belongsToManyRelation')->getPivotFields()
582+
);
583+
584+
return $relation;
585+
});
586+
})
587+
->toArray(),
588+
],
589+
[
590+
'belongs_to_many_relation' => [],
591+
],
592+
]
593+
);
594+
}
595+
596+
public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot_with_null_value(): void
597+
{
598+
$matchingModel = ModelFactory::new()
599+
->hasAttached(
600+
BelongsToManyRelationFactory::new()->count(1),
601+
['number' => null],
602+
'belongsToManyRelation'
603+
)
604+
->create()->fresh();
605+
606+
$matchingModel2 = ModelFactory::new()
607+
->hasAttached(
608+
BelongsToManyRelationFactory::new()->count(1),
609+
['number' => 1],
610+
'belongsToManyRelation'
611+
)
612+
->create()->fresh();
613+
614+
$pivotAccessor = $matchingModel->belongsToManyRelation()->getPivotAccessor();
615+
616+
Gate::policy(Model::class, GreenPolicy::class);
617+
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);
618+
619+
$response = $this->post(
620+
'/api/models/search',
621+
[
622+
'search' => [
623+
'includes' => [
624+
[
625+
'relation' => 'belongsToManyRelation',
626+
'filters' => [
627+
['field' => 'models.pivot.number', 'operator' => '=', 'value' => null],
628+
],
629+
],
630+
],
631+
],
632+
],
633+
['Accept' => 'application/json']
634+
);
635+
636+
$this->assertResourcePaginated(
637+
$response,
638+
[$matchingModel, $matchingModel2],
639+
new ModelResource(),
640+
[
641+
[
642+
'belongs_to_many_relation' => $matchingModel->belongsToManyRelation()
643+
->orderBy('id', 'desc')
644+
->get()
645+
->map(function ($relation) use ($pivotAccessor) {
646+
return collect($relation->only(
647+
array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor])
648+
))
649+
->pipe(function ($relation) use ($pivotAccessor) {
650+
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
651+
->only(
652+
(new ModelResource())->relation('belongsToManyRelation')->getPivotFields()
653+
);
654+
655+
return $relation;
656+
});
657+
})
658+
->toArray(),
659+
],
660+
[
661+
'belongs_to_many_relation' => [],
662+
],
663+
]
664+
);
665+
}
524666
}

tests/Support/Database/migrations/2023_05_00_000000_create_belongs_to_many_relation_model_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function up()
1616
$table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation::class)->constrained(indexName: 'belongs_to_many_relation_self_id_foreign');
1717
$table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\Model::class)->constrained();
1818

19-
$table->integer('number')->default(0);
19+
$table->integer('number')->default(0)->nullable();
2020

2121
$table->timestamps();
2222

tests/Support/Rest/Resources/BelongsToManyResource.php

+2
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)