2017-04-03 32 views
1

私はlaravelのデータベースの累積値を計算する方法を探しています。laravelの累積値の計算

現在、私は毎日1つの値のコレクションを取得しています。私はページ上の累積値を表示できるようにしたいと思います。ここで enter image description here

私は、フォームから製作し、勃起の値を盗ん:私は私のコントローラ内のコード

enter image description here

このようなものがあるしたいです。私はあなたが単にビューでこれを行うことができます

+1

あなたのコードを投稿してください! –

+0

私は質問を編集しました。あなたはそれを確認してください。 –

+0

コードは画像ではなくコードとして投稿してください。 – apokryfos

答えて

2

...毎日のために製作し、勃起値の累積計算したい:

<?php $cumulative = 0; ?> 

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

良い提案 –

+0

このようなロジックは、ビュー上で実行すべきではありません。ビューを通ると、$ collection-> each(function(){})の形式も使えるということです。 – James

+0

@Jamesなぜですか?単純な反復です。もちろん、ビューに渡す前にデータを準備することはできますが、このような簡単な操作ではデータを作成しません。 –

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> 

だったん急いでも動作します。