Laravel - 如何在刀片视图中计算数组中对象的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48325043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:16:09  来源:igfitidea点击:

Laravel - how to calculate the sum of objects in an array in blade view

phplaravel

提问by Leff

I have a foreach loop in a blade view, where I am listing the objects that have a pivot property price:

我在刀片视图中有一个 foreach 循环,我在其中列出了具有枢轴属性价格的对象:

                @foreach($transaction->options as $option)
                  <div class="row">
                    <div class="col-md-6">
                      <p>Option description: {{ $option->description }}</p>
                      <p>Price: {{ $option->pivot->price }}</p>
                    </div>
                  </div>
                  <hr>
                @endforeach
                <div class="row">
                  <div class="col-md-6">
                    <h4>Options total: </h4>
                  </div>
                </div>

I would like to since I am doing a foreach loop here, calculate the sum of all the options so that I could write it next to Options total:

我想,因为我在这里做一个 foreach 循环,计算所有选项的总和,这样我就可以把它写在 Options total:

I have tried with this:

我试过这个:

               @foreach($transaction->options as $option)
                  <div class="row">
                    <div class="col-md-6">
                      <p>Option description: {{ $option->description }}</p>
                      <p>Price: {{ $option->pivot->price }}</p>
                      @php($total += $option->pivot->price)
                    </div>
                  </div>
                  <hr>
                @endforeach
                <div class="row">
                  <div class="col-md-6">
                    <h4>Options total: {{ $total }}</h4>
                  </div>
                </div>

But, that didn't work, I got an error:

但是,这不起作用,我收到了一个错误:

Undefined variable: total

未定义变量:总计

How can I do this?

我怎样才能做到这一点?

回答by Alexey Mezenin

Define the variable first:

首先定义变量:

@php($total = 0);

You can also get sum of pivot column with:

您还可以通过以下方式获得数据透视列的总和:

$transaction->options()->sum('price')

回答by CHARITRA SHRESTHA

You can simply do the calculations in the blade template. This can be helpful for other users. Just do the calculation inside blade syntax {{ }} and use the display:none; css property. For example

您可以简单地在刀片模板中进行计算。这对其他用户很有帮助。只需在刀片语法 {{ }} 中进行计算并使用 display:none; css 属性。例如

<div style="display: none">
 {{ $total = 0 }}
</div>
@foreach($report as $device)
    <tr>
        <td>{{$device->id}}</td>
        <td>{{$device->price}}</td>
        <div style="display: none">{{$total += $device->price}}</div>
    </tr>
@endforeach
<tr>
    <th>Total Distance Travelled</th>
    <th>{{$total}}</th>
</tr>

回答by hlscalon

Blade has a @phpdirective to execute a block of plain PHP within your template. Be aware that this may be a sign of bad design, and you should avoid put much logic inside your template. Ie:

Blade 有一个@php指令来在你的模板中执行一段普通的 PHP。请注意,这可能是糟糕设计的标志,您应该避免在模板中放置太多逻辑。IE:

@php
    $total = 0;
@endphp

@foreach ($items as $item)
<tr>
    <td>{{ $item->name }}</td>
    <td>{{ $item->code }}</td>
    <td>{{ $item->price }}</td>
</tr>

@php
    $total += $item->price;
@endphp

@endforeach

....

<p>Total {{ $total }}</p>