[12.x] Fix: Make Paginated Queries Consistent Across Pages#55176
Merged
taylorotwell merged 2 commits intoMar 27, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When using
Illuminate\Database\Query\Buildermethods orderedChunkById and orderedLazyById without specifying$columnand$aliasproperty values, where the query includes a join on another table, the query for the first page may work as expected while the query for subsequent pages may throwIlluminate\Database\QueryExceptionwith the following message:The cause of this issue is that the query for the first page doesn't include a where clause for the id column, but the subsequent queries do. So it may not be clear that the column is ambiguous until at least the query for the second page is run.
Impact
In non-production environments, since it's common to encounter a smaller dataset, use of these methods without the mentioned properties may work without a problem. However, when releasing a feature into an environment with a large enough dataset to cause the paged query to run more than once, suddenly a working, tested change may fail.
Solution
The PR modifies the forPageBeforeId and forPageAfterId methods in
Illuminate\Database\Query\Builderto always include a where clause on the given$columnproperty; specifically, if property$lastIdevaluates tonull, the clause$this->whereNotNull($column)is added.Benefits
The benefit of always including a where clause on the given column name is that any ambiguity in the query is immediately evident, even when used on a small dataset.