Commit fcd90b9
committed
[12.x] Apply eager load constraints to oneOfMany inner subquery
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 #593181 parent 8aa511e commit fcd90b9
1 file changed
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
945 | 945 | | |
946 | 946 | | |
947 | 947 | | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
948 | 961 | | |
949 | 962 | | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
950 | 979 | | |
951 | 980 | | |
952 | 981 | | |
| |||
0 commit comments