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

anonymouse703's avatar

How to get permissions based on role id using spatie-permission?

Hi, I have successfully stored role-permission but I got stuck on edit/update side.

this is my edit function.

I put a listener roleId from my table component to get the selected role_id in my edit.

public $roleId, $name;
public $permission = [];

protected $listeners = [
        'roleId',
    ];

//edit function
    public function roleId($roleId){
        $this->roleId = $roleId;
        $roles = Role::find($roleId)->with('permissions'); // but this is error
    }

0 likes
5 replies
anonymouse703's avatar

There no permissions

Spatie\Permission\Models\Role {#1499 ▼
  #guarded: array:1 [▶]
  #connection: "mysql"
  #table: "roles"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:5 [▶]
  #original: array:5 [▼
    "id" => 1
    "name" => "Superadmin"
    "guard_name" => "web"
    "created_at" => null
    "updated_at" => null
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "permissions" => Illuminate\Database\Eloquent\Collection {#453 ▼
      #items: []
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  -permissionClass: null
}

I need to show/sync from this table role_has_permissions

frankielee's avatar

Erm, I know you want to show/sync, but you want to save what?

Create new permission and assign it to the role?

Example

$yourPermission= Permission::create(['name' => 'your permission']);

Role::find($roleId)->syncPermissions($yourPermission);

Edit: Can you check your database? You mentioned you saved them successfully, are you using the correct role id? You already managed to get the role and its permissions

anonymouse703's avatar

this is my whole code

<?php

namespace App\Http\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

use Livewire\Component;

class RolePermissionForm extends Component
{
    public $roleId, $name;
    public $permission = [];

    protected $listeners = [
        'roleId',
        'resetInputFields'
    ];

    public function resetInputFields(){
        $this->reset();
    }

    public function render()
    {
        $permissions = Permission::get();
        return view('livewire.role-permission-form')
                ->with('permissions',$permissions);
    }

    //kini ang sa pag update na id
    public function roleId($roleId){
        $this->roleId = $roleId;
        $role = Role::find($roleId)->syncPermissions($this->permission);   
        // $this->name = $roles->name;
        dd($role);
    }

    public function store(){

        $action = '';

        $datas = $this->validate([
            'roleId' => 'required',
            'permission' => 'required'
        ]);


        if($this->roleId){
            foreach($datas as $data){
                $role = Role::findOrFail($datas['roleId']);

                $role->givePermissionTo($datas['permission']);
            }
            $action = 'edit';
        }else{
            foreach($datas as $data){
                $role = Role::findOrFail($datas['roleId']);
                $role->givePermissionTo($datas['permission']);
            }
            $action = 'store';
        }
        $this->emit('showEmitedFlashMessage', $action);
        $this->resetInputFields();
        $this->emit('refreshParent');
        $this->emit('closePermissionRoleModal');

    }
}

The next scenario is what if that specific role want to update it's permission so how can show all the permission based on the role id I selected to this function

//edit
    public function roleId($roleId){
        $this->roleId = $roleId;
        $role = Role::find($roleId)->syncPermissions($this->permission);   // don't know if it's right
        $this->name = $roles->name;
    }
frankielee's avatar

The next scenario is what if that specific role want to update its permission so how can show all the permission based on the role id I selected to this function

The code is right, but you need to make sure the permission is created.

Please or to participate in this conversation.