Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

User1980's avatar

How to get Spattie role name from construct?

Hi,

Any idea how to get the Spattie role name from the construct please?

This is not working:

    private $authorizedUser;
    private $roles;

    public function __construct(Request $request)
    {
        $this->middleware('auth');
        $this->authorizedUser = Auth::user();
        $this->roles = $this->authorizedUser->getRoleNames();
    }

I keep getting: Call to a member function getRoleNames() on null

Thanks for your help

0 likes
4 replies
CorvS's avatar
CorvS
Best Answer
Level 27

@user1980 You are trying to access the authenticated user in the constructor of your controller. That's not gonna work.

User1980's avatar

Thanks, I see, so I have to access it in every single methods instead....

Please or to participate in this conversation.