如何在laravel视图中添加子项总计以获得groupitem总计

I am working on a trial balance page where I want am displaying totals of subgroup items. I want to display a group total. However, I am using {{ }} in the views to display the totals which are coming from the controller

@foreach($sage_paylater_staging as $sg)
 @if( $sg->credit_code == $ledgers->id && $sg->credit_code != '')
    {{ number_format($sg->amount, 2) }}
@endif
@endforeach

This is a code for the subitem total. I want to add the various subitems i.e the amount in the {{ }} and display a total in my group. How could I achieve this?

I have searched numerous websites but there is no way in which I could add the values in the {{ }} in the view. Could anyone help me out?

dsfdgdsfd23212
dsfdgdsfd23212 求和foreach中的值&if并将其存储在变量中。在这里,您无法使用{{}}刀片注释。你必须使用@php标签
一年多之前 回复
doudao1837
doudao1837 我尝试使用map()方法,但它没有给出预期的结果。它不会选择预期的条目。它只选择整个列并创建一个总和,而我想要的是单个列,然后将不同的实体相加
一年多之前 回复
dongshou9878
dongshou9878 在将该字段传递给视图之前,必须将该字段附加到逻辑中的每个项目。您可以使用集合上的方法map()或transform()来实现它。
一年多之前 回复

1个回答

Something like:

@php $total = 0; @endphp
@foreach($sage_paylater_staging as $sg)
    @if( $sg->credit_code == $ledgers->id && $sg->credit_code != '')
        @php $total += $sg->amount; @endphp
        {{ number_format($sg->amount, 2) }}
    @endif
@endforeach
{{number_format($total, 2)}}

But ideally you'd do that logic in your controller and pass the total as a var to the view.

dpg2905
dpg2905 谢谢! 这对我有用
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问