ROLE & USER laravel 8 livewire

What seems to be the problem:

Hi, I am using Spatie and livewire in laravel 8.

to recover the role permissions to a checkbox works fine, but when I want to edit by marking it off and proceed to another, it is automatically marked

Steps to Reproduce:
in Role-component

public function edit($id)
{
    $this->default();
    $editar = Role::find($id);
    $this->rol_id = $editar->id;
    $this->name = $editar->name;

    $this->permissions = Permission::get();
    $this->rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id", $this->rol_id)
        ->pluck('role_has_permissions.permission_id', 'role_has_permissions.permission_id')
        ->all();

    foreach ($this->permissions as $permission) {
        if (in_array($permission->id, $this->rolePermissions))
            array_push($this->permission, $permission->id);
    }
    $this->view = 'edit';
}

in blade:

@foreach($permisos as $value)

        <label class="inline-flex items-center">

            <input type="checkbox" value="{{ $value->id }}"   wire:model="permission" class="form-checkbox h-4 w-4 text-green-500">

            <span class="ml-3 text-sm">{{ $value->name }}</span>

        </label>

        <br />

        @endforeach

Do you have any screenshots or code examples:

hi, i would split things here,
first a method to display the role and another to save it.

Also to avoid running over and over to the server, I would use wire:model.defer=“check.{{$value->id}}”"
Declare the variable as array
Example Public $check=[];
Look in wire model I’m using defer cause for this is not necessary go to server in each check, also look that I’m using variable.id, in this way at the moment of saving you will get an array of all the values selected, you can also use $loop->index to get an ordered array.
[input name=“permissions” wire:model.defer=“check.{{$value->id}}” type=“checkbox” value="{{ $value->id }}" class=“form-checkbox h-4 w-4 text-green-500”]

One important thing is validate everything before save. with the approach above will work, already test it

Livewire:
public $chek = [];
public function edit($id)

{

    $this->default();
    $editar = Role::find($id);
    $this->rol_id = $editar->id;
    $this->name = $editar->name;
    $this->permissions = DB::table("role_has_permissions")
        ->where("role_has_permissions.role_id", $this->rol_id)
        ->pluck('role_has_permissions.permission_id', 'role_has_permissions.permission_id')
        ->all();
    $this->view = 'edit';
}

in blade:

        @foreach($permisos as $value)
        <label class="inline-flex items-center">
            <input  wire:model.defer="chek.{{$value->id}}" name="chek.{{$value->id}}" type="checkbox" value="{{ $value->id }}" class="form-checkbox h-4 w-4 text-green-500"
              @foreach($permissions as $permisos_rol)
                @if($permisos_rol == $value->id)
                    checked 
                @endif
        @endforeach    
        >                      
            <span class="ml-3 text-sm">{{ $value->name }}</span>
        </label>
        <br />            
        @endforeach
    </div>

However, it does not mark the check when I recover.

What I can do?

The logic above works fine with common laravel forms but with livewire is a little different

the input is bind to a variable and for that input show as checked that variable must have the value assign it
Look this little examples that works fine, you can place it in the mount method , using your own logic
public function mount(){
$permisions=[[‘value’=>1],[‘value’=>3]];
for($i=0;$i<=10;$i++) // I’m using 10 here, but you will replace that with items count for check inputs
{
foreach ($permisions as $permision)
{
if($permision[‘value’]===$i) {
$this->check[$i] = $permision[‘value’]; // this will bind the correct value to variable in my case is check.index, like this wire:model.defer=“check.{{$loop->index}}”
}
}
}
}
if you look is the same that you are trying to do but instead of doing in blade, you assign the variable with the desire value in the livewire method,

1 Like

I got the same problem, and could not solve the issue.

Can someone please explain how to handle form edit, from a model with checkboxes.

It seems existing data is not bind with the wire:model properly.

Hi, Share your code to see, cause for value to be bind, the array must have a value in the position that you are trying to bind, check the example i wrote above.

Here i wrote how to use spatie permissions package with livewire
https://forum.laravel-livewire.com/t/how-to-implement-spatie-permissions-package-with-livewire/4360/2