Skip to content

Commit 03d2533

Browse files
committed
✅ nullable pivot filtering
1 parent c63d975 commit 03d2533

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php

+72
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,78 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
562562
['Accept' => 'application/json']
563563
);
564564

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+
565637
$this->assertResourcePaginated(
566638
$response,
567639
[$matchingModel, $matchingModel2],

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

0 commit comments

Comments
 (0)