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
use this code to display sum of specific column
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