PJunior PJunior - 4 months ago 39
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

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;