Skip to content

[13.x] Fix ofMany() alias inside closures - #62051

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:one-of-many-closure-relation-name
Oct 16, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:one-of-many-closure-relation-name

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

ofMany() uses the calling function's name as the join alias. On PHP 8.4 a closure defined outside a class is named like {closure:/var/www/routes/web.php:12}, and the dot breaks the query:

User::resolveRelationUsing('bestPost', function ($user) {
    return $user->hasOne(Post::class)->ofMany('score', 'max');
});

$user->bestPost; // syntax error near "."

On PHP 8.3 closures were just named {closure}, so this used to work.

Now the related table name is used when the guessed name is a closure.

@taylorotwell
taylorotwell merged commit e0b8135 into laravel:13.x Oct 16, 2026
62 checks passed
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