我正在寻找一种方法来计算在laravel数据库的累积值。
目前,我检索一个值集合,每天一个。我希望能够在页面上显示累积值。我想要这样的东西

我的控制器中的代码是:

在这里,我从形式中找回制造和架设的价值。我想要计算每天的制造和架设的累积价值...
发布于 2017-04-03 21:13:28
您可以简单地在视图中执行此操作:
<?php $cumulative = 0; ?>
@foreach ($collection as $object)
<?php $cumulative += $object->value; ?>
{{ $object->value }} | {{ $cumulative }}
@endforeach发布于 2017-12-13 15:21:51
创建一个函数,然后执行该函数
public function CumulativeCalculator($accounts){
$driverAccounts = [];
$cumulativePayments = 0;
foreach($accounts as $account){
$cumulativePayments += $account->value;
$currentDriverAccounts = [];
$currentDriverAccounts['id'] = $account->id;
$currentDriverAccounts['realValue'] = $account->value;
$currentDriverAccounts['cumulativeValue'] = $cumulativePayments;
array_push($driverAccounts,$currentDriverAccounts);
}
return $driverAccounts;
}在您的视图中执行此操作
<table class="table table-hover table-striped">
<thead>
<th>Values</th>
<th>Cumulative Values</th>
</thead>
<tbody>
@foreach($driverAccounts as $dataValue)
<tr>
<td>{{$dataValue['realValue']}}</td>
<td>{{$dataValue['cumulativeValue']}}</td>
</tr>
@endforeach
</tbody>
</table>这对你很管用,很抱歉我很着急,但它会管用的。
https://stackoverflow.com/questions/43185507
复制相似问题