[13.x] Fix `has()` and `withCount()` on `HasOneThrough` one-of-many relations
Merged
XU
1mo ago+9+9+0123456789-1-1-0123456789
Querying the existence of a HasOneThrough relation defined with latestOfMany() or ofMany() fails on MySQL and Postgres. This covers has(), whereHas(), whereRelation(), withCount(), withExists() and doesntHave(). The one-of-many subquery gets joined before the intermediate table, so its on clause points at a table that isn't in the query yet.
For example, with User::latestLogin() defined as hasOneThrough(Login::class, Device::class)->latestOfMany(), calling User::has('latestLogin')->get() throws Unknown column 'devices.user_id' in 'on clause'. Eager and lazy loading work fine, only the existence queries break.