From 50fd3c6d387dafa12bc47b19be8b06e8146b2a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Nikolaou?= Date: Wed, 28 Oct 2020 02:17:56 +0200 Subject: [PATCH] Wrap column in aggregate function (#35004) --- .../Database/Eloquent/Concerns/QueriesRelationships.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 2dce87ae4cd1..d2589c46bf45 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -381,11 +381,15 @@ public function withAggregate($relations, $column, $function = null) $relation = $this->getRelationWithoutConstraints($name); + $expression = $function + ? sprintf('%s(%s)', $function, $this->getGrammar()->wrap($column)) + : $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 // sub-query. We'll format this relationship name and append this column if needed. $query = $relation->getRelationExistenceQuery( - $relation->getRelated()->newQuery(), $this, new Expression($function ? "$function($column)" : $column) + $relation->getRelated()->newQuery(), $this, new Expression($expression) )->setBindings([], 'select'); $query->callScope($constraints);