Maybe you can share the foreach?
How do i get a running total from the previous transaction?
Hi All
I'm trying to get a running total sum in my view from my transactions table which holds transactions for different customers using a foreign key. In the view the transactions are then filtered down by the user selecting the relevant customer.
For reference, i have a tx_amount_in field which records the values added and a tx_amount_out field which records the values deducted.
In my view, i have a @foreach looping over each iteration of the transactions and after each transaction i would like to show the value remaining whether its added or deducted but for the life of me i cant figure it out.
So far i've got it giving me the sum of the last transaction OR another way i tried wasn't calculating correctly at all.
@forelse($transactions as $tx)
<tr>
@php
$runningTotal = 0;
$runningTotal += $tx->tx_amount_in;
$runningTotal -= $tx->tx_amount_out;
@endphp
<td class="table-c-row card-title">
@if ($runningTotal >= 0)
<p>£{{ $runningTotal }} remaining</p>
@else
<p>£{{ abs($runningTotal) }} excess</p>
@endif
</td>
<td class="table-c-row">
@if($tx->tx_in != 0)
<div class="card-title-alt text-right text-green"> <i class="fad fa-coin mr-1"></i> {{ number_format($tx->tx_amount_in, 2) }}</div>
@else
<div class="card-title-alt text-right text-red"> <i class="fad fa-coin mr-1"></i>- {{ number_format($tx->tx_amount_out, 2) }}</div>
@endif
</td>
</tr>
@endforeach
Please can someone advise how best to tackle this. Any questions, please let me know.
Thanks in advance.
Please or to participate in this conversation.
