Reputation: 91
In Laravel, if I want to get the total price of all products in the cart, what should I add to my code? This is the cart.blade.php file:
@extends('layouts.app')
@section('content')
<main class="container">
<section class="row justify-content-center">
<h1 class="text-center mb-5">Products on your cart:</h1>
</section>
<section class="row justify-content-center">
@foreach(Auth::user()->computers()->get() as $computer)
<div class="col-12 col-md-4">
<h2 class="text-center">{{$computer->name}}</h2>
<img class="card-img-top" src="{{Storage::url($computer->img)}}" alt="">
<form action="{{route('computers.removepc',['id'=>$computer->id])}}" method="POST">
@csrf
<center><button class="btn btn-danger mb-5" type="submit">Remove</button></center>
<p class="text-center">Price: {{$computer->price}}</p> //This is the single price of each product
</div>
@endforeach
</section>
<section class="row justify-content-center">
<h1>Here I want the total price</h1> //Here is , for example, where i want the total price of all computers in the cart
</section>
</main>
@endsection
Thanks for help!
Upvotes: 1
Views: 956
Reputation: 523
Try to add $total varibale above @foreach, like this:
<?php $total = 0; ?>
Then inside @foreach add the following:
<?php $total+= $computer->price ?>
Then go to the place you want to output the total and write:
{{$total}}
Upvotes: 2