laravel select2 livewire component code example
Example: laravel livewire select2 multi select
public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];
public function locationUsersSelected($locationUsersValues)
{
$this->locationUsers = $locationUsersValues;
}
<div class="col-md-12 mb-3" wire:ignore>
<label for="locationUsers">Select Users</label>
<select id="locationUsers" class="form-control select2" multiple="multiple">
<option value="">--select--</option>
@foreach($this->users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
@endforeach
</select>
</div>
document.addEventListener('livewire:load', function () {
$('#locationUsers').on('select2:select', (e) => {
@this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
});
$('#locationUsers').val(@this.get('locationUsers')).trigger('change');
});