Skip to content

Commit

Permalink
Merge pull request #986 from rasmuscnielsen/patch-field-selection-on-…
Browse files Browse the repository at this point in the history
…belongstomany-relations

Fix selecting fields on belongs to many relations
  • Loading branch information
freekmurze authored Dec 23, 2024
2 parents d12e6b7 + e5af869 commit 52749b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Includes/IncludedRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __invoke(Builder $query, string $relationship)
}

return [$fullRelationName => function ($query) use ($fields) {
$query->select($fields);
$query->select($query->qualifyColumns($fields));
}];
})
->toArray();
Expand Down
28 changes: 25 additions & 3 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
$this->assertQueryLogContains('select `name` from `related_models`');
$this->assertQueryLogContains('select `related_models`.`name` from `related_models`');
});

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

$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
$this->assertQueryLogContains('select `name` from `related_models`');
$this->assertQueryLogContains('select `related_models`.`name` from `related_models`');
});

it('can fetch only requested string columns from an included belongs to many model', function () {
TestModel::first()->relatedThroughPivotModels()->create([
'name' => 'related',
]);

$request = new Request([
'fields' => 'id,related_through_pivot_models.id,related_through_pivot_models.name',
'include' => ['relatedThroughPivotModels'],
]);

$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedFields('id', 'related_through_pivot_models.id', 'related_through_pivot_models.name')
->allowedIncludes('relatedThroughPivotModels');

DB::enableQueryLog();

$queryBuilder->first()->relatedThroughPivotModels;

$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
$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 (');
});

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

$this->assertQueryLogContains('select * from `test_models`');
$this->assertQueryLogContains('select `id`, `name` from `related_models`');
$this->assertQueryLogContains('select `related_models`.`id`, `related_models`.`name` from `related_models`');
});

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

0 comments on commit 52749b2

Please sign in to comment.