Skip to content

Commit

Permalink
Merge pull request #144 from Lomkit/feature/include-filtering-on-pivot
Browse files Browse the repository at this point in the history
✅ include filtering on pivot
  • Loading branch information
GautierDele authored Dec 2, 2024
2 parents 473601f + 6481540 commit 35d0021
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,146 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
]
);
}

public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot(): void
{
$matchingModel = ModelFactory::new()
->hasAttached(
BelongsToManyRelationFactory::new()->count(1),
['number' => 3],
'belongsToManyRelation'
)
->create()->fresh();

$matchingModel2 = ModelFactory::new()
->hasAttached(
BelongsToManyRelationFactory::new()->count(1),
['number' => 1],
'belongsToManyRelation'
)
->create()->fresh();

$pivotAccessor = $matchingModel->belongsToManyRelation()->getPivotAccessor();

Gate::policy(Model::class, GreenPolicy::class);
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);

$response = $this->post(
'/api/models/search',
[
'search' => [
'includes' => [
[
'relation' => 'belongsToManyRelation',
'filters' => [
['field' => 'models.pivot.number', 'operator' => '>', 'value' => 2],
],
],
],
],
],
['Accept' => 'application/json']
);

$this->assertResourcePaginated(
$response,
[$matchingModel, $matchingModel2],
new ModelResource(),
[
[
'belongs_to_many_relation' => $matchingModel->belongsToManyRelation()
->orderBy('id', 'desc')
->get()
->map(function ($relation) use ($pivotAccessor) {
return collect($relation->only(
array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor])
))
->pipe(function ($relation) use ($pivotAccessor) {
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
->only(
(new ModelResource())->relation('belongsToManyRelation')->getPivotFields()
);

return $relation;
});
})
->toArray(),
],
[
'belongs_to_many_relation' => [],
],
]
);
}

public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot_with_null_value(): void
{
$matchingModel = ModelFactory::new()
->hasAttached(
BelongsToManyRelationFactory::new()->count(1),
['number' => null],
'belongsToManyRelation'
)
->create()->fresh();

$matchingModel2 = ModelFactory::new()
->hasAttached(
BelongsToManyRelationFactory::new()->count(1),
['number' => 1],
'belongsToManyRelation'
)
->create()->fresh();

$pivotAccessor = $matchingModel->belongsToManyRelation()->getPivotAccessor();

Gate::policy(Model::class, GreenPolicy::class);
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);

$response = $this->post(
'/api/models/search',
[
'search' => [
'includes' => [
[
'relation' => 'belongsToManyRelation',
'filters' => [
['field' => 'models.pivot.number', 'operator' => '=', 'value' => null],
],
],
],
],
],
['Accept' => 'application/json']
);

$this->assertResourcePaginated(
$response,
[$matchingModel, $matchingModel2],
new ModelResource(),
[
[
'belongs_to_many_relation' => $matchingModel->belongsToManyRelation()
->orderBy('id', 'desc')
->get()
->map(function ($relation) use ($pivotAccessor) {
return collect($relation->only(
array_merge((new BelongsToManyResource())->getFields(app()->make(RestRequest::class)), [$pivotAccessor])
))
->pipe(function ($relation) use ($pivotAccessor) {
$relation[$pivotAccessor] = collect($relation[$pivotAccessor]->toArray())
->only(
(new ModelResource())->relation('belongsToManyRelation')->getPivotFields()
);

return $relation;
});
})
->toArray(),
],
[
'belongs_to_many_relation' => [],
],
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up()
$table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation::class)->constrained(indexName: 'belongs_to_many_relation_self_id_foreign');
$table->foreignIdFor(\Lomkit\Rest\Tests\Support\Models\Model::class)->constrained();

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

$table->timestamps();

Expand Down
2 changes: 2 additions & 0 deletions tests/Support/Rest/Resources/BelongsToManyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lomkit\Rest\Http\Requests\RestRequest;
use Lomkit\Rest\Http\Resource;
use Lomkit\Rest\Relations\BelongsTo;
use Lomkit\Rest\Relations\BelongsToMany;
use Lomkit\Rest\Tests\Support\Models\BelongsToManyRelation;

class BelongsToManyResource extends Resource
Expand All @@ -18,6 +19,7 @@ public function relations(RestRequest $request): array
{
return [
BelongsTo::make('model', ModelQueryChangedResource::class),
BelongsToMany::make('models', ModelResource::class)->withPivotFields(['number']),
];
}

Expand Down

0 comments on commit 35d0021

Please sign in to comment.