简体   繁体   中英

Laravel total of products from Pivot table

 @foreach($order->products as $product)

                    <tr>

                        <td> {{ $product->id }}</td>
                        <td> {{ $product->name }}</td>
                        <td> {{ $product->getRelationValue('pivot')->qty }} </td>
                        <td> {{ $product->getRelationValue('pivot')->price }} </td>
                        <td> {{ $total = $product->getRelationValue('pivot')->price * $product->getRelationValue('pivot')->qty }} </td>
                    </tr>

                @endforeach
Grand Total:{{$total}}

I would like to show the total value. Can someone help me out? And also the random access to $product->id in format 1,2,3....

You can do something like below code:

@php
$grnadTotal = 0;
@endphp

 @foreach($order->products as $product)
    <tr>
       <td>{{ $product->id }}</td>
       <td>{{ $product->name }}</td>
       <td>{{ $product->getRelationValue('pivot')->qty }}</td>
       <td>{{ $product->getRelationValue('pivot')->price }}</td>
       <td>{{ $total = $product->getRelationValue('pivot')->price * $product->getRelationValue('pivot')->qty }}</td>
    </tr>

   @php
      $grnadTotal += $total;
   @endphp
@endforeach

Grand Total:{{$grnadTotal}}

use this

Grand Total: {{$order->products->count()}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM