rabin senchuri
rabin senchuri

Reputation: 317

Calculating cumulative values in laravel

I am looking for a way to calculate cumulative of values of database in laravel.

Currently I retrieve a collection of values, one for each day. I would like to be able to show the cumulative value on the page. I want something like this

enter image description here

Code in my controller is : enter image description here

Here I retreive the values of fabrication and erection from form. I want to calculate cumulative of fabrication and erection value for each day...

Upvotes: 1

Views: 2490

Answers (2)

Tongi
Tongi

Reputation: 51

Create a function and follow with that

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;

}

In your View do this

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

That will work for you, sorry i was in a hurry but it will work.

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

You can simply do this in the view:

<?php $cumulative = 0; ?>

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

Upvotes: 2

Related Questions