PJunior PJunior - 1 year ago 151
PHP Question

Laravel - How to get Entrust Roles of a specific user

I'm making a small work with Laravel and using Zizaco Entrust.

While logged in as Administrator I want to see all Roles of a specific user.

I'v searched for a while but didn't find any clue...
How can I do it using Entrust or shall I use SQL queries?

Answer Source

In your User class add

public function roles()
    return $this->belongsToMany('Role','assigned_roles');

Then you can get all roles for a specific user

$user = User::with('roles')->find(1);
$roles = $user->roles;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download