Skip to content

Commit 52749b2

Browse files
authored
Merge pull request #986 from rasmuscnielsen/patch-field-selection-on-belongstomany-relations
Fix selecting fields on belongs to many relations
2 parents d12e6b7 + e5af869 commit 52749b2

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Includes/IncludedRelationship.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __invoke(Builder $query, string $relationship)
2828
}
2929

3030
return [$fullRelationName => function ($query) use ($fields) {
31-
$query->select($fields);
31+
$query->select($query->qualifyColumns($fields));
3232
}];
3333
})
3434
->toArray();

tests/FieldsTest.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
$queryBuilder->first()->relatedModels;
175175

176176
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
177-
$this->assertQueryLogContains('select `name` from `related_models`');
177+
$this->assertQueryLogContains('select `related_models`.`name` from `related_models`');
178178
});
179179

180180
it('can fetch only requested string columns from an included model', function () {
@@ -197,7 +197,29 @@
197197
$queryBuilder->first()->relatedModels;
198198

199199
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
200-
$this->assertQueryLogContains('select `name` from `related_models`');
200+
$this->assertQueryLogContains('select `related_models`.`name` from `related_models`');
201+
});
202+
203+
it('can fetch only requested string columns from an included belongs to many model', function () {
204+
TestModel::first()->relatedThroughPivotModels()->create([
205+
'name' => 'related',
206+
]);
207+
208+
$request = new Request([
209+
'fields' => 'id,related_through_pivot_models.id,related_through_pivot_models.name',
210+
'include' => ['relatedThroughPivotModels'],
211+
]);
212+
213+
$queryBuilder = QueryBuilder::for(TestModel::class, $request)
214+
->allowedFields('id', 'related_through_pivot_models.id', 'related_through_pivot_models.name')
215+
->allowedIncludes('relatedThroughPivotModels');
216+
217+
DB::enableQueryLog();
218+
219+
$queryBuilder->first()->relatedThroughPivotModels;
220+
221+
$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
222+
$this->assertQueryLogContains('select `related_through_pivot_models`.`id`, `related_through_pivot_models`.`name`, `pivot_models`.`test_model_id` as `pivot_test_model_id`, `pivot_models`.`related_through_pivot_model_id` as `pivot_related_through_pivot_model_id` from `related_through_pivot_models` inner join `pivot_models` on `related_through_pivot_models`.`id` = `pivot_models`.`related_through_pivot_model_id` where `pivot_models`.`test_model_id` in (');
201223
});
202224

203225
it('can fetch requested array columns from included models up to two levels deep', function () {
@@ -299,7 +321,7 @@
299321
$queryBuilder->first()->relatedModels;
300322

301323
$this->assertQueryLogContains('select * from `test_models`');
302-
$this->assertQueryLogContains('select `id`, `name` from `related_models`');
324+
$this->assertQueryLogContains('select `related_models`.`id`, `related_models`.`name` from `related_models`');
303325
});
304326

305327
it('wont use sketchy field requests', function () {

0 commit comments

Comments
 (0)