I'm using Livewire 3. I want to pass some values to a function. Here are the related codes:
<div x-data="{ selectedDataTypes: [] }" class="space-x-0.5 space-y-1.5">
<input type="checkbox" class="tag-checkbox" id="assetclass" x-model="selectedDataTypes" value="assetclass">
<label class="tag" for="assetclass">Assets</label>
<input type="checkbox" class="tag-checkbox" id="costcenter" x-model="selectedDataTypes" value="costcenter">
<label class="tag" for="costcenter">Costs</label>
<input type="checkbox" class="tag-checkbox" id="funcarea" x-model="selectedDataTypes" value="funcarea">
<label class="tag" for="funcarea">Functional Areas</label>
<input type="checkbox" class="tag-checkbox" id="fund" x-model="selectedDataTypes" value="fund">
<label class="tag" for="fund">Funds</label>
<button wire:click="$dispatch('syncSelected', selectedDataTypes)" class="btn-primary my-2">Sync Selected</button>
<!-- Loading spinner -->
<svg wire:loading wire:target="syncSelected" class="animate-spin ml-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</div>
Livewire component:
class Data extends Component
{
public $selectedDataTypes = [];
#[On('syncSelected')]
public function syncSelected(DataService $service)
{
// Check if none is selected
if (empty($this->selectedDataTypes)) {
dd($this->selectedDataTypes);
session()->flash('error', 'Please select at least one data type to sync.');
return; // Early return to stop the execution here
}
try {
$service->syncSelected($this->selectedDataTypes);
session()->flash('success', 'Selected data synced successfully.');
} catch (\Exception $e) {
session()->flash('error', $e->getMessage());
}
}
}
Why am I getting an empty array [] in the dd()? Should I be using something else other than $dispatch? I've been going through the documentation for the last two hour with no progress. Please help. Thank you.