Skip to content

CurrentUser incorrectly resolves empty User instance. #53267

@anned20

Description

@anned20

Laravel Version

11.22.0

PHP Version

8.2.16

Database Driver & Version

No response

Description

We are using the new ContextualAttribute to get the CurrentUser in our controller methods. This works fine, until there is no user in the guard.

The function resolve in the attribute correctly returns null, but the Container then resolves the parameter to a new User instance if there is a type-hint for a User on the parameter. This even happens when the parameter type is indicated as optional.

Steps To Reproduce

class SomeController { public function someMethod( #[CurrentUser] ?User $user ): void { dd($user); } }

When executing this code without being logged in (having no available user), the $user will contain an empty model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions