mercredi 29 novembre 2023

Sum total and return it when checkboxes checked with laravel livewire

I want to sum data from database where I can get it when checkbox is checked.

this is my component livewire

class YpReconSpv extends Component
{
    public $checkboxes = [];
    public $selected = [];
    public $total = 0;

    public function updatedCheckboxes($value)
    {

        if ($value) {
            $sales = YamahaPointSale::find($value);
            if ($sales) {
                $this->selected[$value] = $sales->total_amount;
            }
            $this->selected[$value] = $sales->total_amount;
        } else {
            if (isset($this->selected[$value])) {
                unset($this->selected[$value]);
            }
        }

        $this->total = array_sum(array_values($this->selected));
    }

    public function render()
    {
        $sales = YamahaPointSale::get();

        return view('livewire.yamaha.yp-recon-spv', compact('sales'));
    }
}

what I try to reach is when I click the checkbox, I get data from database, then I populate it in an associative array then sum it.

and this is my view

<input type="checkbox" wire:model="checkboxes." name="id[]"
id="id" value="" wire:change="updatedCheckboxes()">

but when I try it, it return error




Aucun commentaire:

Enregistrer un commentaire