计算laravel中的累积值

时间:2017-04-03 13:09:10

标签: php laravel

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

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

enter image description here

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

在这里,我从形式上了解制造和安装的价值。我想计算每天累积的制造和安装值......

2 个答案:

答案 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>

这对你有用,对不起,我很着急,但它会奏效。