Livewire Checkboxes with wire:model don’t work

Recently I found with this issue on Livewire when I add an property to a wire:model as an array of ids it automatically transform the content into integers.

$this->roles = [1,4,5];

After clicking on the checkboxes it become something like this:

$this->roles = [1,4,5,"1"];

The issue is caused by the Javascript compare for Array.includes() where 1 == “1” returns false.

Here is a better explaining about this:

So the proposed solution on this thread is to put on the assignation something like this:

$this->roles = array_map('strval', [1,4,5] );

In order to have the values as strings instead of integers.

Leave a Comment