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
}
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
<?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;
}
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.