[英]Laravel foreach total price of all products
在 Laravel 中,如果我想獲取購物車中所有產品的總價,我應該在我的代碼中添加什么? 這是 cart.blade.php 文件:
@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
感謝幫助!
嘗試在@foreach 上方添加 $total 變量,如下所示:
<?php $total = 0; ?>
然后在@foreach 中添加以下內容:
<?php $total+= $computer->price ?>
然后go到你想要的地方output總寫:
{{$total}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.