Select Multiple or Checkboxes

How to set the values from existing data
I see many questions on Laracasts where the person seems confused about how to set the value of the select input or checkboxes when using Laravel Livewire.
The simple answer is that you don't need to worry if you set your component up right then Laravel will take care of it for you.
The component illustrates the use of both select (with "multiple" enabled), and checkboxes.
The code for the component looks like
Livewire/RolesTest.php (part)
1
<?php
2
3
namespace App\Http\Livewire;
4
5
use App\Models\User;
6
use Livewire\Component;
7
use Spatie\Permission\Models\Role;
8
9
class RolesTest extends Component
10
{
11
public int $user_id;
12
public string $user_name;
13
public array $userRoles;
14
15
public function mount(User $user)
16
{
17
$this->user_id = $user->id;
18
$this->user_name = $user->name;
19
20
$this->userRoles = $user->roles()->pluck('id')->toArray();
21
}
22
23
public function render()
24
{
25
return view('livewire.roles-test')
26
->withRoles(
27
cache()->remember('roles',60, function(){
28
return Role::all();
29
})
30
);
31
}
32
33
protected $rules = [
34
'userRoles.*' => 'exists:roles,id',
35
];
36
37
public function submit()
38
{
39
$this->validate();
40
41
$user = User::findOrFail($this->user_id);
42
43
$user->roles()->sync($this->userRoles);
44
45
}
46
47
}
48
The important points are;
Line 13, the user's existing roles are pulled from the user model roles relationship, and stored in the component as an array
Line 26, all possible roles are passed to the view. Here, the cache is used to keep a record of the possible options in the cache for 60 seconds. So if the component is changed multiple times, then the roles are not re-queried from the database each time.
Line 34, since we are holding the choices as an array, we can use Laravel's array validation to ensure that each member of the array matches the rules, and in this case, the role must exist in the roles table.
Line 43, once the roles have been chosen, since they are an array of IDs, they can be simply passed into the sync() function against the roles relationship. No matter what the roles before the save, syncing the roles with the array will set the new roles only.

The view

Select box with multiple option

The view contains both input types for the benefit of this article only. I suggest that you use checkboxes over the multi-select because of the tricky UI to select multiple, and not accidentally clearing existing roles.
roles-test.blade.php (part)
1
{{-- Method using multi-select input --}}
2
<select multiple wire:model.lazy="userRoles" class="w-1/2 rounded form-multiselect">
3
@foreach($roles as $role)
4
<option value="{{$role->id}}">{{$role->name}}</option>
5
@endforeach
6
</select>
7
<div class="text-sm italic text-gray-600">Alt/Cmd click to select multiple roles</div>
8
For the Multiple Select, we need to wire:model the select to the roles array then iterate over the possible roles for each option. We do NOT need to use the selected html parameter on each option as this will be applied automatically by Livewire' wire:model.
Neither do we need to worry about the old() helper, again Livewire will take care of this for us.

The Checkbox Option

1
{{-- method using checkboxes --}}
2
<div class="flex flex-col my-8 space-y-1">
3
@foreach($roles as $role)
4
<div class="flex justify-between">
5
<label for="role-{{$role->name}}">{{$role->name}}</label>
6
<input class="rounded form-checkbox" id="role-{{$role->name}}"
7
type="checkbox" value="{{$role->id}}" wire:model.lazy="userRoles" />
8
</div>
9
@endforeach
10
</div>
For each of the possible roles, we output a label and checkbox. Each individual checkbox is bound to the array of the user roles. Livewire takes care of changing the correct array member according to the value of the checkbox.
Again, we don't need to care about the old() helper or the checked state

Beware of user manipulation of public component properties

With Livewire2, you should be wary of a user manipulating the public properties of your component, such as changing the user_id to that of another user. Check the video;
You can discuss pages on this site at https://github.com/snapey/talltips/discussions