Jan 2, 2021
0
Level 6
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.
Please or to participate in this conversation.