From b0dc0b2432d426349009a70775fa469d16ea1a14 Mon Sep 17 00:00:00 2001 From: Daniel Bakan Date: Mon, 2 Nov 2020 10:54:21 +0100 Subject: [PATCH] fix qualified column in withAggregate --- .../Concerns/QueriesRelationships.php | 14 ++++++++-- .../Database/DatabaseEloquentBuilderTest.php | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 4a3930932bca..708df8736d84 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -381,9 +381,17 @@ public function withAggregate($relations, $column, $function = null) $relation = $this->getRelationWithoutConstraints($name); - $expression = $function - ? sprintf('%s(%s)', $function, $this->getQuery()->getGrammar()->wrap($column)) - : $column; + if ($function) { + $expression = sprintf( + '%s(%s)', + $function, + $this->getQuery()->getGrammar()->wrap( + $column === '*' ? $column : $relation->getRelated()->qualifyColumn($column) + ) + ); + } else { + $expression = $column; + } // Here, we will grab the relationship sub-query and prepare to add it to the main query // as a sub-select. First, we'll get the "has" query and use that to get the relation diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index a5253f1d293a..41108073fcc3 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -816,6 +816,24 @@ public function testWithCountAndGlobalScope() $this->assertSame('select "id", (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" limit 1) as "foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql()); } + public function testWithMin() + { + $model = new EloquentBuilderTestModelParentStub; + + $builder = $model->withMin('foo', 'price'); + + $this->assertSame('select "eloquent_builder_test_model_parent_stubs".*, (select min("eloquent_builder_test_model_close_related_stubs"."price") from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" limit 1) as "foo_min_price" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql()); + } + + public function testWithMinOnBelongsToMany() + { + $model = new EloquentBuilderTestModelParentStub; + + $builder = $model->withMin('roles', 'id'); + + $this->assertSame('select "eloquent_builder_test_model_parent_stubs".*, (select min("eloquent_builder_test_model_far_related_stubs"."id") from "eloquent_builder_test_model_far_related_stubs" inner join "user_role" on "eloquent_builder_test_model_far_related_stubs"."id" = "user_role"."related_id" where "eloquent_builder_test_model_parent_stubs"."id" = "user_role"."self_id" limit 1) as "roles_min_id" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql()); + } + public function testWithCountAndConstraintsAndHaving() { $model = new EloquentBuilderTestModelParentStub; @@ -1482,6 +1500,16 @@ public function activeFoo() { return $this->belongsTo(EloquentBuilderTestModelCloseRelatedStub::class, 'foo_id')->where('active', true); } + + public function roles() + { + return $this->belongsToMany( + EloquentBuilderTestModelFarRelatedStub::class, + 'user_role', + 'self_id', + 'related_id' + ); + } } class EloquentBuilderTestModelCloseRelatedStub extends Model