计算laravel中的累积值

问题描述:

我正在寻找一种方法来计算laravel中数据库的累积值。计算laravel中的累积值

目前我检索一组值,每天一个。我希望能够在页面上显示累计值。我想是这样的

enter image description here

代码在我的控制器是: enter image description here

在这里,我retreive制造和安装的值从形式。我想计算累计制造和每天勃起值...

+1

发表您的代码! –

+0

我编辑了这个问题..可以ü请检查它 –

+0

将您的代码发布为代码而不是图像 – apokryfos

你可以简单的做到这一点的看法:

<?php $cumulative = 0; ?> 

@foreach ($collection as $object) 
    <?php $cumulative += $object->value; ?> 
    {{ $object->value }} | {{ $cumulative }} 
@endforeach 
+0

好的建议 –

+0

这样的逻辑不应该在视图上完成,它应该至少在控制器上完成并通过视图,这意味着你也可以使用$ collection-> each(function(){})格式。 – James

+0

@詹姆斯为什么不呢?这是一个简单的迭代。当然,您可以在将数据传递给视图之前准备数据,但是我不会那么做这样简单的操作。 –

创建功能,并按照与

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> 

这将为你工作,对不起我在匆忙但它会奏效。