Php 获取来自api的表列的总金额之和

Php 获取来自api的表列的总金额之和,php,laravel,api,Php,Laravel,Api,我有一个带有唯一编号的api,编号不同,所以当我用这个唯一编号调用api时,我会返回数据,我会在表中显示数据。该表有一个列名“amount”。 由于它的动态数据来自一个api,我只是不知道如何在表的最底部显示总量。我用的是拉威尔 我的代码示例 <table class="table table-one"> <thead class="table-success"> <tr>

我有一个带有唯一编号的api,编号不同,所以当我用这个唯一编号调用api时,我会返回数据,我会在表中显示数据。该表有一个列名“amount”。 由于它的动态数据来自一个api,我只是不知道如何在表的最底部显示总量。我用的是拉威尔

我的代码示例

<table class="table table-one">
       <thead class="table-success">
            <tr>
                <th scope="col">Inst. No.</th>
                <th scope="col">PR No.</th>
                <th scope="col">PR Date</th>
                 <th scope="col">Prem. Amt.</th>
            </tr>
        </thead><!-- ends: thead -->
        <tbody>

         @foreach ($results['polinfo'] as $data) 
          @foreach ($data['poldata'] as $res)
            <tr>
                <th scope="row">{{$res['INSTALNO']}}</th>
                <td>{{$res['PR_NO']}}</td>
                <td>{{$res['PR_DATE']}}</td>
                <td>{{$res['AMOUNT']}}</td>
            </tr>
       @endforeach
         @endforeach

        </tbody><!-- ends: tbody -->
    </table>

仪器编号。
公共关系编号。
公关日期
预付金额。
@foreach($results['polinfo']作为$data)
@foreach($data['poldata']作为$res)
{{$res['INSTALNO']}
{{$res['PR_NO']}
{{$res['PR_DATE']}
{{$res['AMOUNT']}
@endforeach
@endforeach
我必须计算所有{$res['AMOUNT']}},并将其显示为总金额


提前感谢。

如果您仅在
foreach
之后需要该值,您可以使用单独的变量来添加金额:

@php($amount = 0)

@foreach ($results['polinfo'] as $data) 
    @foreach ($data['poldata'] as $res)
        <tr>
            <th scope="row">{{$res['INSTALNO']}}</th>
            <td>{{$res['PR_NO']}}</td>
            <td>{{$res['PR_DATE']}}</td>
            <td>{{$res['AMOUNT']}}</td>
        </tr>
        
        @php($amount += (int) $res['AMOUNT'])
    @endforeach
@endforeach

Amount: {{ $amount }}
@php($amount=0)
@foreach($results['polinfo']作为$data)
@foreach($data['poldata']作为$res)
{{$res['INSTALNO']}
{{$res['PR_NO']}
{{$res['PR_DATE']}
{{$res['AMOUNT']}
@php($amount+=(int)$res['amount']))
@endforeach
@endforeach
金额:{{$Amount}

总计应添加到foreach循环之后,这对于一般编程是正确的。最外层foreach后的总金额,以及最内层foreach后的小计金额。