Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mifta13's avatar

Sum total data from database with livewire when checkbox is checked

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.{{ $index }}" name="id[]"
id="id" value="{{ $sale->id }}" wire:change="updatedCheckboxes({{ $sale->id }})">
0 likes
1 reply
tangtang's avatar

@mifta13

use wire:click instead of wire:change

the wire:change directive is used when binding to form elements like input fields.

for checkboxes, it's better to use wire:click

Please or to participate in this conversation.