Skip to content

[13.x] Fix has() and withCount() on HasOneThrough one-of-many relations - #61818

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:has-one-through-of-many-existence
Oct 2, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:has-one-through-of-many-existence

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

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.

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.

2 participants