skip to Main Content

I want to display the sum total of all credit activities in a user account as his main account balance. I have three different wallet, bonus, referral and deposit and will like to sum up all of them together. This is already being recorded individually in the transaction history but I want the total sum to appear in the user dashboard.

id deposit bonus referral balance
10 200 15 5
13 320 21 null

The balance of user (10) is the sum total of deposit + bonus + referral, which is suppose to be 220 while user 13 balance is 341. How can this be achieve to display for each user?

Here is my model

`public function transactions()
{
    return $this->hasMany(Transaction::class)->orderBy('id','desc');
}`

This is usercontroller

`public function transactions()
{
    $pageTitle = 'Transactions';
    $remarks   = Transaction::distinct('remark')->orderBy('remark')->get('remark');

    $transactions = Transaction::where('user_id', auth()->id())->searchable(['trx'])->filter(['trx_type', 'remark', 'wallet_type'])->orderBy('id', 'desc')->paginate(getPaginate());
    return view($this->activeTemplate . 'user.transactions', compact('pageTitle', 'transactions', 'remarks'));
}`

Blade

 <li>
    <span class="caption">@lang('Main Balance')</span>
    <span class="value">{{ showAmount($transaction->post_balance) }}</span>
</li>

2

Answers


  1. use this code to display sum of specific column

    <span class="value">{{ showAmount($transaction->sum('post_balance')) }}</span>
    
    Login or Signup to reply.
  2. So far i know about the problem you may use laravel built-in sum method to perform sum of deposit, bonus and referral for each row

    Not tested but probably true

    public function transactions()
    {
        $pageTitle = 'Transactions';
        $remarks   = Transaction::distinct('remark')->orderBy('remark')->get('remark');
    
        $userTransactions = Transaction::where('user_id', auth()->id())->searchable(['trx'])->filter(['trx_type', 'remark', 'wallet_type'])->orderBy('id', 'desc');
        $transactions = $userTransactions->paginate(getPaginate());
    
        $totalBalance = $userTransactions->get()->sum(function (array $transaction) {
             return (int) transaction->deposit + (int) transaction->bonus + (int) transaction->referral;
        });
    
        
    return view($this->activeTemplate . 'user.transactions', compact('pageTitle', 'transactions', 'remarks', 'totalBalance'));
    }
    
     <li>
        <span class="caption">@lang('Main Balance')</span>
        <span class="value">{{ showAmount($totalBalance) }}</span>
    </li>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search