[11.x] Support for multiple partition by
columns in groupLimit()
#52072
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.
This PR builds on the "eager loading with limit" functionality introduced in #49695.
It allows
groupLimit()
to accept an array of columns, which will add multiple columns to thepartition by
part of the query.This is immediately achieved just by changing from
wrap()
tocolumnize()
inGrammar::compileRowNumber()
. The rest of the changes in this PR are to support the MySQL legacy workaround and of course testing.Use Case
The use case for this is to be able to use
groupLimit()
on its own to get a limit of n records from each group, grouped by multiple columns (not just the foreign key in a relationship).For example, say I want to get the latest address of each type for each user:
This generates the following SQL:
Relationships
You can also use
groupLimit()
in a relationship definition:Or indeed in the original eager-loading scenario:
Both of these are similar to
ofMany()
to convert a HasMany relationship into a HasOne. But instead, it will still return multiple records for each parent, but only one from each group (in this example, one of each address type).Future State
I had a bit of a think about whether, on the
*Many
relationship classes, Laravel magic could take care of the foreign key and only the additional column(s) would need to be manually specified, i.e. the waylimit()
currently sendsgetExistenceCompareKey()
/getQualifiedFirstKeyName()
togroupLimit()
.This could be done either by adding a new optional
$columns
parameter tolimit()
or a new method altogether, but I thought I'd start with the basic change first and work from there.Let me know if there is any interest in this and any preferences for how best to implement it.