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

lyndonjohn's avatar

Get Newly Selected Checkbox with Pre-Selected Values

I'm trying to update the Roles of a certain User which currently has Manager and Staff roles.

I have three roles:

[ ] Admin
[ ] Manager
[ ] Staff

I can display their current roles with this code in my Livewire class:

public $selectedRoles = [];

public function mount()
{
    $this->roles = Role::orderBy('label')->get();
    $this->selectedRoles = $this->user->roles->pluck('id')->toArray();
}

Blade view:

@foreach ($roles as $role)
    <div class="custom-control custom-checkbox mb-5">
        <input
            wire:model="selectedRoles"
            class="custom-control-input
            type="checkbox" id="{{$role->label}}"
            value="{{$role->id}}">
        <label class="custom-control-label text-capitalize"
               for="{{$role->label}}">{{$role->label}}</label>
    </div>
@endforeach

However, if I will uncheck Manager, and check Admin, the Manager role gets automatically checked back. Maybe because of this code $this->selectedRoles = $this->user->roles->pluck('id')->toArray(); and $selectedRoles is wired to my checkboxes.

I can update the user's roles using vanilla Laravel. Is there a way to achieve this using Livewire?

Thanks.

0 likes
0 replies

Please or to participate in this conversation.