[12.x] Apply eager load constraints to oneOfMany inner subquery#59333
Closed
sadique-cws wants to merge 1 commit intolaravel:13.xfrom
Closed
[12.x] Apply eager load constraints to oneOfMany inner subquery#59333sadique-cws wants to merge 1 commit intolaravel:13.xfrom
sadique-cws wants to merge 1 commit intolaravel:13.xfrom
Conversation
When eager loading a latestOfMany/ofMany relationship with constraints
(e.g. with(['lastPayment' => fn($q) => $q->where('store_id', 1)])),
the constraints were only applied to the outer query, not the inner
subquery that determines which record is the 'latest'.
This caused the inner subquery to pick the wrong aggregate row (e.g.
the latest payment across ALL stores), and then the outer filter would
discard it — silently returning null.
The fix snapshots the outer query's wheres before and after the user's
constraint closure runs, then copies only the new wheres into the
oneOfMany subquery. This ensures the inner subquery determines 'latest'
within the user's filtered dataset.
Fixes laravel#59318
Member
|
I am closing this pull request because it lacks sufficient explanation, tests, or both. It is difficult for us to merge pull requests without these things because the change may introduce breaking changes to the framework. Feel free to re-submit your change with a thorough explanation of the feature and tests - integration tests are preferred over unit tests. Please include it's benefit to end users; the reasons it does not break any existing features; how it makes building web applications easier, etc. Thanks! |
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.
Fixes #59318.
Problem
When eager loading a latestOfMany / ofMany relationship with constraints (e.g. with(['lastPayment' => fn($q) => $q->where('store_id', 1)])), the constraints were only being applied to the outer query, not the inner subquery that determines which record is the "latest".
This caused the inner subquery to pick the wrong aggregate row (e.g. the latest payment across all stores), and then the outer filter would discard it—silently returning
null.Solution
In
Eloquent\Builder::eagerLoadRelation(), we now snapshot the outer query'swheresandbindingsbefore and after the user's constraint closure runs. If the relation is a one-of-many relation, we copy those newly added wheres directly into the inneroneOfManySubQuery.This ensures the inner subquery correctly determines "latest" or "oldest" within the user's filtered dataset, matching the behavior that users expect when adding constraints to eager loads. All existing eloquent relation tests pass.