首页 > 解决方案 > Laravel - 我需要在哪里放置我的 sum()

问题描述

这个变量可以向我显示与sub_account相关的任何发票的数据

我在发票中有数据,这是我可以正确显示的金额。但我需要显示具有相同 sub_account 的所有金额的总和。如下图所示,每张发票都会出现 2 次问题。

这是我的代码:查看:

 @foreach($invoices as $invoice)
    @foreach($invoice->subaccount as $sub_accounts)
       <tr>
              @foreach($sub_accounts->subaccountinvoice as $sub_account)
                  <td scope="row">{{$sub_account->sum('amount')}}</td>
               @endforeach
        </tr>                   
    @endforeach
 @endforeach

在控制器中:

 $invoices = Account::with(['subaccount.subaccountinvoice])->where('id',$id)->get();

谢谢你。

总和的2倍

标签: laravelsum

解决方案


只需将 $sum 变量传递给您的视图文件。

return view('yourView')->with(['sumAmount' => $sum]);

然后在视图文件中回显变量 $sumAmount:

{{$sumAmount}}

推荐阅读