Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fix infinite loop when global scopes query contains aggregates #49972

Merged
merged 1 commit into from Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -630,8 +630,8 @@ public function withAggregate($relations, $column, $function = null)
$relation = $this->getRelationWithoutConstraints($name);

if ($function) {
if ($this->getGrammar()->isExpression($column)) {
$aggregateColumn = $this->getGrammar()->getValue($column);
if ($this->getQuery()->getGrammar()->isExpression($column)) {
$aggregateColumn = $this->getQuery()->getGrammar()->getValue($column);
} else {
$hashedColumn = $this->getRelationHashedColumn($column, $relation);

Expand All @@ -642,7 +642,7 @@ public function withAggregate($relations, $column, $function = null)

$expression = $function === 'exists' ? $aggregateColumn : sprintf('%s(%s)', $function, $aggregateColumn);
} else {
$expression = $this->getGrammar()->getValue($column);
$expression = $this->getQuery()->getGrammar()->getValue($column);
}

// Here, we will grab the relationship sub-query and prepare to add it to the main query
Expand Down Expand Up @@ -671,7 +671,7 @@ public function withAggregate($relations, $column, $function = null)
// the query builder. Then, we will return the builder instance back to the developer
// for further constraint chaining that needs to take place on the query as needed.
$alias ??= Str::snake(
preg_replace('/[^[:alnum:][:space:]_]/u', '', "$name $function {$this->getGrammar()->getValue($column)}")
preg_replace('/[^[:alnum:][:space:]_]/u', '', "$name $function {$this->getQuery()->getGrammar()->getValue($column)}")
);

if ($function === 'exists') {
Expand Down