php - 如何从 foreach 循环中对刀片文件中的 linetotal 求和
问题描述
我想总结刀片文件上的标记变量。我在数据库中的表上没有行总计字段。我在总行中找到总和数量和价格,但我想总结所有行总和。
<?php
$sl = 1;
?>
<tbody>
@foreach($qInvoice as $sInvoice)
<tr>
<td>{{$sl++}}</td>
<td>{{$sInvoice->catName}}</td>
<td>{{$sInvoice->proName}}</td>
<td>{{$sInvoice->qty}}</td>
<td>{{$sInvoice->meName}}</td>
<td class="text-right">{{$sInvoice->price}}/-</td>
<td class="text-right">{{$sInvoice->price * $sInvoice->qty}}/-</td>
<?php
$total = 0;
$lineTotal = $sInvoice->price * $sInvoice->qty;
$total+=$lineTotal;
?>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<td colspan="5"></td>
<td class="text-right">Total</td>
<td class="text-right">{{$total}}/-</td>
</tr>
</tfoot>
</table>
我想在不使用控制器的情况下对总字段中的所有行总计求和
解决方案
不要在视图中求和,在控制器中进行,例如:
$qInvoice->transform(function($sInvoice) {
$sInvoice->line_total = $sInvoice->qty * $sInvoice->price;
return $sInvoice;
});
$total = $qInvoice->sum($line_total);
并返回带有 $total 和 $qInvoice 的视图!
推荐阅读
- python - 为什么我的函数要修改输入变量?
- angular - 是否可以在不使用 npm 的情况下导入 Angular 库?
- python - 如何使用 PocketSphinx 识别 Python 中的多个关键字?
- css - wordpress woocommerce 消息变色
- amazon-web-services - 当我尝试从两个不同的终端选项卡轮询相同的 Amazon SQS 时,我在这两个选项卡中都没有收到相同的消息
- html - 如何从重叠元素中删除框阴影
- xaml - NotifyPropertyChange 已触发,但 Xamarin.Forms 中的 UI 字段未更新
- reactjs - withAuthenticator 注册完成
- sql - 想知道是否可以进一步简化此 SQL 查询以确定数据集最后日期的最高 stackoverflow 帖子
- python-3.x - Dockerfiles 构建性能问题