Can't bind selected checkbox values with livewire component

Viewed 79

I want to get all checkbox values as an array.

Blade:

<button wire:click.prevent="export">
   Export selected
</button>
<div>
@foreach($hotels as $hotel)
   <input type="checkbox" wire:model="selectedRows" value="{{ $hotel->id }}">
   <label>{{ $hotel->name }}</label>
@endforeach
</div>

Component:

public $selectedRows = [];

public function export() {
   dd($this->selectedRows);
}

The result I get is an empty array. Any suggestions?

1 Answers

The solution for me was to put a button also inside the div tag:

<div>
   <button wire:click.prevent="export">
      Export selected
   </button>
   @foreach($hotels as $hotel)
      <input type="checkbox" wire:model="selectedRows" value="{{ $hotel->id }}">
      <label>{{ $hotel->name }}</label>
   @endforeach
</div>
Related