我正在寻找一种计算laravel中数据库累积值的方法。
目前我检索了一组值,每天一个。我希望能够在页面上显示累积值。我想要这样的东西
我的控制器中的代码是:
在这里,我从形式上了解制造和安装的价值。我想计算每天累积的制造和安装值......
答案 0 :(得分:2)
您可以在视图中执行此操作:
<?php $cumulative = 0; ?>
@foreach ($collection as $object)
<?php $cumulative += $object->value; ?>
{{ $object->value }} | {{ $cumulative }}
@endforeach
答案 1 :(得分:1)
创建一个函数并按照
进行操作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>
这对你有用,对不起,我很着急,但它会奏效。