Level 34
@user1980 make sure you added the HasRoles trait in your User model
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
@user1980 You are trying to access the authenticated user in the constructor of your controller. That's not gonna work.
Please or to participate in this conversation.