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

LaCoder's avatar

Get all permissions from role

Hello,

I am using Laravel Permissions from https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage

I have created roles and permissions, but now I am editing the role which was created. So now I need to pull all permissions which are assigned to a role,

$role = Role::where('roles.id', $role_id)
        ->leftjoin('role_has_permissions', 'role_has_permissions.role_id', '=', 'roles.id')
        ->leftjoin('permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
        ->select('permissions.*', 'roles.*')
        ->get();

But i can not get 3rd table data of permissions, what i want is, role details + all permissions to that role.

Thanks,

0 likes
3 replies
SilenceBringer's avatar

@lacoder role has permissions relationship, so, just eager load it

$role = Role::where('roles.id', $role_id)
        ->with('permissions')
        ->get();

and the you can access it as

$role->permissions
1 like
LaCoder's avatar

@silencebringer when i dd($role->permissions) it say message: "Property [permissions] does not exist on this collection instance."

When i dd($role) i can see all dump with permissions as well,

I want to loop this $role to check with existing loop, how can i do that?

@foreach ($role as $role_l)
    @foreach ($vendor_permissions as $vendor_permission)
    <td style="width: 15%"><label class="checkbox"><input type="checkbox" name="permissions[]" id="{{ $vendor_permission->name }}" 
        {{ $vendor_permission->id === $role_l->permissions->permission_id ? 'selected="selected"' : '' }} value="{{ $vendor_permission->id }}""><span></span></label></td> 
    @endforeach   
@endforeach

Please or to participate in this conversation.