Help, I work with laravel and spatie for roles and permissions. I want to edit my permissions with a checkbox list from a window Attached code:
En el componente:
public $role;
public $rol_id, $name;
public $chek = [];
public function mount(Role $role)
{
$this->role = $role;
$this->data = $role->toArray();
$this->chek = $this->role->permissions()->pluck('id')->toArray();
}
protected $rules =[
'role.name' => 'required'
];
public function render()
{
$permisos = Permission::all();
return view('roles.edit-rol-component', compact('permisos'));
}
public function update(){
$role = $this->role;
$this->validate([
'role.name' => 'required',
]);
$this->role->save();
$role->syncPermissions(['permission'=> $this->chek]);
$this->emit('alert','success','El rol se editó correctamente.');
$this->closeModalWithEvents([
RolComponent::getName() => 'renderEvent']);
}
In the view, the list generator shows me but it marks the checks wrong
@foreach($permisos as $key => $value)
<label class="inline-flex items-center">
<input wire:model.defer="chek.{{$value->id}}"
type="checkbox" value="{{ $value->id }}"
class="form-checkbox h-4 w-4 text-green-500"
@if($role->permissions->contains($value->id)) checked @endif >
<span class="ml-3 text-sm">{{ $value->name }} </span>
</label>
@endforeach