Skip to content

Commit f182cea

Browse files
authored
Merge branch 'main' into bugfix/eager-loading-search
2 parents 63b6adc + bea6a08 commit f182cea

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

src/Http/Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function modelToResponse(Model $model, Resource $resource, array $request
109109
})
110110
->toArray(),
111111
collect($model->getRelations())
112-
->mapWithKeys(function ($modelRelation, $relationName) use ($requestArray, $relation, $resource) {
112+
->mapWithKeys(function ($modelRelation, $relationName) use ($currentRequestArray, $relation, $resource) {
113113
$key = Str::snake($relationName);
114114

115115
if (is_null($modelRelation)) {
@@ -132,15 +132,15 @@ public function modelToResponse(Model $model, Resource $resource, array $request
132132
$key => $this->modelToResponse(
133133
$modelRelation,
134134
$relationResource,
135-
$requestArray,
135+
$currentRequestArray,
136136
$relationConcrete
137137
),
138138
];
139139
}
140140

141141
return [
142142
$key => $modelRelation
143-
->map(fn ($collectionRelation) => $this->modelToResponse($collectionRelation, $relationResource, $requestArray, $relationConcrete))
143+
->map(fn ($collectionRelation) => $this->modelToResponse($collectionRelation, $relationResource, $currentRequestArray, $relationConcrete))
144144
->toArray(),
145145
];
146146
})

tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function test_getting_a_list_of_resources_including_relation_with_unautho
9393
'search' => [
9494
'includes' => [
9595
[
96-
'relation' => 'hasManyRelation.model',
96+
'relation' => 'hasManyRelation.belongsToRelation',
9797
],
9898
[
9999
'relation' => 'hasManyRelation',

tests/Feature/Controllers/SearchSelectingOperationsTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Illuminate\Support\Facades\Gate;
66
use Lomkit\Rest\Tests\Feature\TestCase;
7+
use Lomkit\Rest\Tests\Support\Database\Factories\HasManyRelationFactory;
78
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
9+
use Lomkit\Rest\Tests\Support\Models\HasManyRelation;
810
use Lomkit\Rest\Tests\Support\Models\Model;
911
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
1012
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource;
@@ -89,4 +91,64 @@ public function test_getting_a_list_of_resources_selecting_two_fields(): void
8991
['id', 'number']
9092
);
9193
}
94+
95+
public function test_getting_a_list_of_resources_deep_selecting_fields(): void
96+
{
97+
$matchingModel = ModelFactory::new()->has(HasManyRelationFactory::new()->count(2), 'hasManyRelation')->create()->fresh();
98+
$matchingModel2 = ModelFactory::new()->create()->fresh();
99+
100+
Gate::policy(Model::class, GreenPolicy::class);
101+
Gate::policy(HasManyRelation::class, GreenPolicy::class);
102+
103+
$response = $this->post(
104+
'/api/models/search',
105+
[
106+
'search' => [
107+
'selects' => [
108+
['field' => 'id'],
109+
],
110+
'includes' => [
111+
[
112+
'relation' => 'hasManyRelation',
113+
'selects' => [
114+
['field' => 'id'],
115+
],
116+
'includes' => [
117+
[
118+
'relation' => 'model',
119+
'selects' => [
120+
['field' => 'id'],
121+
],
122+
],
123+
],
124+
],
125+
],
126+
],
127+
],
128+
['Accept' => 'application/json']
129+
);
130+
131+
$this->assertResourcePaginated(
132+
$response,
133+
[$matchingModel, $matchingModel2],
134+
new ModelResource(),
135+
[
136+
[
137+
'has_many_relation' => $matchingModel->hasManyRelation()
138+
->orderBy('id')
139+
->with('model:id')
140+
->get()
141+
->map(function ($relation) {
142+
$relation->model = $relation->model->toArray();
143+
144+
return $relation->only(['id', 'model']);
145+
})->toArray(),
146+
],
147+
[
148+
'has_many_relation' => [],
149+
],
150+
],
151+
['id', 'has_many_relation']
152+
);
153+
}
92154
}

0 commit comments

Comments
 (0)