Level 13
Found the answer here: https://laracasts.com/discuss/channels/livewire/livewire-checkboxes-array-with-pluck
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I am trying to create a page where a admin can sync user & roles.
public $user;
public $selectedRoles = [];
public function mount()
{
$this->roles = SpatieRole::all();
$this->selectedRoles = $this->user->roles->pluck('id')->toArray();
}
public function updatedSelectedRoles()
{
$this->user->syncRoles($this->selectedRoles);
}
FrontEnd:
<ul>
@foreach($roles as $role)
<li>
<input
type="checkbox"
value="{{ $role->id }}"
wire:model="selectedRoles"
>{{ $role->id }} {{ $role->name }}
</li>
@endforeach
</ul>
This works fine if $selectedRoles is empty array.
Let me know how can I fix this. Thanks.
Found the answer here: https://laracasts.com/discuss/channels/livewire/livewire-checkboxes-array-with-pluck
Please or to participate in this conversation.