Skip to content

[12.x] Apply eager load constraints to oneOfMany inner subquery#59333

Closed
sadique-cws wants to merge 1 commit intolaravel:13.xfrom
sadique-cws:fix/eager-load-one-of-many-subquery-constraints
Closed

[12.x] Apply eager load constraints to oneOfMany inner subquery#59333
sadique-cws wants to merge 1 commit intolaravel:13.xfrom
sadique-cws:fix/eager-load-one-of-many-subquery-constraints

Conversation

@sadique-cws
Copy link
Contributor

Title: [12.x] Apply eager load constraints to oneOfMany inner subquery

Compare: 12.x...sadique-cws:framework:fix/eager-load-one-of-many-subquery-constraints


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's wheres and bindings before 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 inner oneOfManySubQuery.

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.

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
@taylorotwell
Copy link
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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eager load constraints on latestOfMany/ofMany not applied to inner subquery

2 participants