Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0
Auth::user()->role()->name

I think.

0

That throws an error that the function does not exist. I tried the original (Auth:user()->role->name) in an existing application and there it works just fine. (with another relation because I don't have a roles table there.

0

up up up up

0

You should make sure you are authenticated before using Auth::user().

if(Auth::check())
{
    $role = Auth::user()->role->name;
}

codingjames liked this reply

1

Ok I found out why it wasn't working for me. The thing is that my User model where I was talking about was a part of my package, and because Laravel has it's own User model in the default Laravel installation it was not working.

Your package model does not override an already existing model. I solved my problem by making a Trait instead of a model for my package.

codingjames liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

jorenvh jorenvh Joined 13 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.