laravel livewire select2 multi select code example

Example: laravel livewire select2 multi select

######## INSIDE LIVEWIRE COMPONENT
public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];

public function locationUsersSelected($locationUsersValues)
{
  $this->locationUsers = $locationUsersValues;
}

######## INSIDE LIVEWIRE BLADE
<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>

######## INSIDE LIVEWIRE SCRIPTS
document.addEventListener('livewire:load', function () {
  $('#locationUsers').on('select2:select', (e) => {
    @this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
  });

  $('#locationUsers').val(@this.get('locationUsers')).trigger('change');
});

Tags:

Php Example