[12.x] Fix: Make Paginated Queries Consistent Across Pages #55176
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\Builder
methods orderedChunkById and orderedLazyById without specifying$column
and$alias
property 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\QueryException
with 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\Builder
to always include a where clause on the given$column
property; specifically, if property$lastId
evaluates 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.