Get total value from table row price in Laravel

Just started to working with laravel and found it really cool framework to work with , I'm working with basic Shopping cart and after i do Add to Cart Option , product values goto another table called "customer_items" along with current user ID,

now when I want to call current product which user already added to cart and show it in a table , here is the code

            @foreach($shoppingCarts as $items)
              <tr>

                <td  class="name"><a href="#">{{ $items->product_name }}</a></td>

                <td class="price">{{ $items->product_price }}</td>

                <td class="total"> <a href="#"><img class="tooltip-test" data-original-title="Remove"  src="img/remove.png" alt=""></a></td>

              </tr>
            @endforeach

note : {{ $items->product_price }} , usually there is 4, 6 items added by user , I want to get total value of product price from each row ,

Thanks in Advance for the help :) !

Upvotes: 1

Views: 4462

Answers (1)

Used this and worked fine ,

    <?php $total = 0; ?>

        @foreach($shoppingCarts as $items)
                <?php $total += $items->product_price; ?>
                          <tr>

                        <td  class="name"><a href="#">{{ $items->product_name }}</a></td>

                    <td class="price">{{ $items->product_price }}</td>

                    <td class="total">{{ $total }}<a href="#"><img class="tooltip-test" data-original-title="Remove"  src="img/remove.png" alt=""></a></td>

                  </tr>
                @endforeach

or

        @foreach($shoppingCarts as $items)

                          <tr>

                        <td  class="name"><a href="#">{{ $items->product_name }}</a></td>

                    <td class="price">{{ $items->product_price }}</td>

                    <td class="total">{{ $items->sum('product_price') }}<a href="#"><img class="tooltip-test" data-original-title="Remove"  src="img/remove.png" alt=""></a></td>

                  </tr>
                @endforeach

Thanks everyone for the support :) !

Source : https://laracasts.com/discuss/channels/general-discussion/get-total-value-from-table-rows

Upvotes: 1

Related Questions