1

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.

aynber
  • 22,380
  • 8
  • 50
  • 63
BolsaLoca
  • 11
  • 2

1 Answers1

0

problem :
every time you navigate through pagination links, you actually send a get request to the same URL, plus ?page=N (N: link number) E.g. https://example.com/account/36?page=2.
and each time controller should execute, and at the beginning, it set

    $previousTransaction=0;
    $previousBalance=$account->account_current_balance;

solution:
I don't have your complete code, but your controller should be something like this:

    public function ControllerFunction(Request $request){

        $transactions=\App\Transaction::orderByDesc('transaction_date')->orderByDesc('created_at')->where('account_id',$id)->paginate(20);

        if(!$request->has(['pre_trans', 'pre_balance']){
            $previousTransaction=0;
            $previousBalance=$account->account_current_balance;
        }else{
            $previousTransaction = $request['pre_trans'];
            $previousBalance = $request['pre_balance'];
        }

        foreach($transactions as $key => $transaction) {
            $balances[]=$previousBalance-$previousTransaction;
            $previousBalance=array_values($balances)[++$key-1];
            $previousTransaction = $transaction->transaction_ammount;
        }

        if(!$request->has(['pre_trans', 'pre_balance']){
            $request->request->add(['pre_trans' => $previousTransaction]);
            $request->request->add(['pre_balance' => $previousBalance]);
        }else{
            $request['pre_trans'] = $previousTransaction;
            $request['pre_balance'] = $previousBalance;
        }
    }
  • Thanks for the answer. However I have 2 problems: 1.- When adding static to $previousBalance=$account->account_current_balance I have an error "Constant expression contains invalid operations", I have just read that you cannot start a static variable with a variable. 2.- I have the same result with and without static. I have changed $account->account_current_balance for a constant, added static in $previousTransaction and $previousBalance, and the balances do restart when paginating. – BolsaLoca Sep 07 '18 at 05:46
  • sorry for problem#1, I didn't notice that. I saw this [link](https://stackoverflow.com/questions/9497161/how-do-i-change-a-static-variables-value-in-php) and figure out that because evetime it executes the controller the static variable reset and gets the new value.so i think you should use session(request).i'll update the answer hope it will help you. – MashMostafa Sep 07 '18 at 07:22
  • Thanks for the effort! I will take a look later this evening and let you know, – BolsaLoca Sep 07 '18 at 18:19