Mr.SH
Mr.SH

Reputation: 457

Laravel Livewire calculate values on blade

I have one dynamic blade where I put value in three text fields and store that value in array.

enter image description here

my blade look like this

in my Laravel Livewire controller I have bellow code

public $veriProducts = [];

public function render()
    {
        $products = Product::where('user_id', auth()->user()->id)
            ->orderBy( $this->sortBy, $this->sortAsc ? 'ASC' : 'DESC');
 
        $products = $products->paginate($this->perPage);
        
        info($this->veriProducts);

        return view('livewire.products', [
            'items' => $products,
        ]);

    }

  public function addProduct()
    {
        $this->veriProducts[] = ['price' => '', 'quantity' => ''];
    }

    public function removeProduct($index)
    {
        unset($this->veriProducts[$index]);
        $this->veriProducts = array_values($this->veriProducts);
    }

here is my blade

<tbody>
@foreach ($veriProducts as $index => $orderProduct)
<tr>
<td>
<input type="number" name="veriProducts[{{$index}}][price]" class="border border-gray-300 form-control my-0 p-1 ring-blue-200 rounded-md" wire:model="veriProducts.{{$index}}.price" />
</td>
<td>
<input type="number" name="veriProducts[{{$index}}][quantity]" class="border border-gray-300 form-control my-0 p-1 ring-blue-200 rounded-md" wire:model="veriProducts.{{$index}}.quantity" />
</td>

<input type="number" name="veriProducts[{{$index}}][total]" class="border border-gray-300 form-control my-0 p-1 ring-blue-200 rounded-md" wire:model="veriProducts.{{$index}}.total" />
</td>

<input type="number" name="veriProducts[{{$index}}][pl]" class="border border-gray-300 form-control my-0 p-1 ring-blue-200 rounded-md" wire:model="veriProducts.{{$index}}.pl" />
</td>

</tr>
@endforeach

its working fine I am able to add rows and also array $veriProducts is also created fine. How can I calculate total and P/L after quantity text box loose focus or some other

Total will be
price*quanrity

PL will be
20% less then total

Thanks so much

Upvotes: 1

Views: 1840

Answers (1)

Irfan
Irfan

Reputation: 1030

Assuming the PL, and Total are not editable we can use an updated hook and set the total and pl values accordingly,


    public function updatedVeriProducts()
    {
        $this->veriProducts = collect($this->veriProducts)
               ->map(function ($product) {
                   if ($product['price'] != '' && $product['quantity'] != '') {
                       $total = $product['price'] * $product['quantity'];
                       $product['total'] = round($total, 2);
                       $product['pl'] = round($total * 0.8, 2);
                   }
                   return $product;
               })->toArray();
    }

I have made use of the collect method by laravel to work with the array easily.

With the if condition, we enable the calculation of total and pl only when both price and quantity has some input values.

And to avoid long decimals, I rounded it to 2 decimal places with round function.

Upvotes: 2

Related Questions