[10.x] Fix infinite loop when global scopes query contains aggregates #49972
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Attempt to fix #49968.
Pinging @tpetry here because this is related to #49912. I'm not sure how to test this TBH, so I'd appreciate your input here.
Calling
$this->getGrammar()
will redirect the call (passthru) from\Illuminate\Database\Eloquent\Builder
to the baseIlluminate\Database\Query\Builder
. When doing this, we add a call to->applyScopes()
:framework/src/Illuminate/Database/Eloquent/Builder.php
Line 1760 in 4f7802d
This will cause laravel to call the scopes we have defined, and, since we are using a
count
aggregate, we'll end up in thewithAggregate
method defined on line 606 ofIlluminate\Database\Eloquent\Concerns\QueriesRelationships
trait. Now, since we are calling$this->getGrammar()
directly, we'll start the process of forwarding the call to the base query builder and calling->applyScopes
all over again, which will cause an infinite loop.I hope I was clear enough, but help to explain in the comments if need be.