I am really new to laravel and quite new to codding. I am surprise that running totals or balances don't receive a lot of attention.
The problem is:
- I have the current balance of an account recorded in the database.
- I have a page called account/$id, where I get all the transactions of the account with that id.
- My last column is the balance of the account id after that transaction. A reverse running total, I think it is called. Example: balance of transaction 3 = balance of transaction 2 - ammount of transaction 2.
- Until here everything works.
- However, when I enter paginate; only the first page give me the correct balance. In the second the balance restarts from the begining.
Code that works:
Controller
$transactions=\App\Transaction::orderByDesc('transaction_date')->orderByDesc('created_at')->where('account_id',$id)->get();
$previousTransaction=0;
$previousBalance=$account->account_current_balance;
foreach ($transactions as $key => $transaction) {
$balances[]=$previousBalance-$previousTransaction;
$previousBalance=array_values($balances)[++$key-1];
$previousTransaction = $transaction->transaction_ammount;
}
View
@foreach($transactions as $key => $transaction)
<td class="align">{{array_values($balances)[++$key-1]}}</td>
@endforeach
Code that doesn't give me the correct balance:
Controller
$transactions=\App\Transaction::orderByDesc('transaction_date')->orderByDesc('created_at')->where('account_id',$id)->paginate(20);
$previousTransaction=0;
$previousBalance=$account->account_current_balance;
foreach ($transactions as $key => $transaction) {
$balances[]=$previousBalance-$previousTransaction;
$previousBalance=array_values($balances)[++$key-1];
$previousTransaction = $transaction->transaction_ammount;
}
View
@foreach($transactions as $key => $transaction)
<td class="align">{{array_values($balances)[++$key-1]}}</td>
@endforeach
<div class="content-header noevents">
{{ $transactions->links() }}
</div>
I have tryed as well different appends in links(), but it seems that I don't know how to use it or doesn't work.
Really big thanks in advance to anyone that can help me.