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

dpakagarwal's avatar

User roles and permission issue using spatie permission package

as I am using the laravel spatie package for managing users but is not working as I want which is described as follows: I want this type of view : Image as i have created a seeder file for this as shown below:

$permissions = [
            [
                'name' => 'role-list|role-create|role-edit|role-delete',
                'main_group' => 'Role',
                'display_name' => 'All'
            ],
            [
                'name' => 'role-list',
                'main_group' => 'Role',
                'display_name' => 'View'
            ],
            [
                'name' => 'role-create|role-edit',
                'main_group' => 'Role',
                'display_name' => 'Insert|Update'
            ],
            [
                'name' => 'role-delete',
                'main_group' => 'Role',
                'display_name' => 'Delete'
            ],
        ];

        foreach ($permissions as $permission) {
            Permission::create($permission);
        }

all permissions have been assigned to admin auto using seeder file successfully but when I click on the particular menu in the sidebar it shows me a page with unauthorized access or you do not have permission.

this is my sidebar.blade.php file :

@canany(['role-list','role-create','role-edit','role-delete'])
                        @can('role-list')
                            <li><a class="nav-link" href="{{ route('admin.roles.index') }}">Role</a></li>
                        @endcan
                    @endcanany

this is my Role controller file (just trying to do with this file right now)

function __construct()
    {
        $this->middleware('permission:role-list|role-create|role-edit|role-delete', ['only' => ['index', 'create', 'store', 'edit', 'update', 'destroy']]);
        $this->middleware('permission:role-list', ['only' => ['index']]);
        $this->middleware('permission:role-create|role-edit', ['only' => ['create', 'store', 'edit', 'update']]);
        $this->middleware('permission:role-delete', ['only' => ['destroy']]);
    }

this is my main.blade.php file for list view with crud part :

<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
        <div class="breadcrumb-title pe-3">{{ $title }}</div>
        <div class="ms-auto">
            @can('role-create')
                <div class="btn-group">
                    <a href="{{ route('admin.roles.create') }}" class="btn btn-primary">Add New</a>
                </div>
            @endcan
        </div>
    </div>
0 likes
8 replies
CookieMonster's avatar

You should post your code here instead of linking it as a screenshot.

CookieMonster's avatar

@deepakagarwal Two things then:

Fetch all the roles available in your role table and foreach it in the dropdown(as shown in top left of your original picture) so it shows all the roles. Then for each role, you need to check the permissions that the particular role has. In each of the boxes, foreach each of the permission that exists. In the foreach, just check if the role has the particular permission and if yes, set the checkbox to checked.

As for updating the permissions, you can need to keep track of the checked boxes in each of the permission and upon update, just overwrite the permissions for that particular role.

That's pretty much the idea here.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@deepakagarwal Well seems like you just need to have a foreach that goes over every role. That will create the white boxes. The inside each box, you have a foreach on every permission that exists (+ all). You render those as checkboxes. If a role has that permission (->hasPermission()), you set it to checked. Then you have an on-click handler (livewire perhaps?) that updates that roles permission on ciick.

Please or to participate in this conversation.