0

I have a deposit table wherein there are multiple rows display. I have a this foreach loop on my blade file which works totally fine and shows the values like the image

@foreach ($moneytradeDeposits as $mtd)
   <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Deposited</div>
   <div class="h5 mb-0 font-weight-bold text-gray-800">Php {{$mtd->amount}}</div>
@endforeach

enter image description here

But I also have another blade file where in I want to display the sum or total of the values from the amount row. I have tried the codes

<div class="h5 mb-0 font-weight-bold text-gray-800">Php {{$mtd->amount->sum}}</div>

<div class="h5 mb-0 font-weight-bold text-gray-800">Php {{$mtd->amount->sum()}}</div>

<div class="h5 mb-0 font-weight-bold text-gray-800">Php {{$mtd->amount->getTotal()}}</div>

<div class="h5 mb-0 font-weight-bold text-gray-800">Php {{$mtd->amount->total()}}</div>

But none of these seemed to work and I only get errors. by the way, this is my Schema for this table

 Schema::create('money_trade_deposits', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('mt_dep_number');
       
        $table->integer('amount');
        $table->string('payment_method');

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->timestamps();


    });

And my Controller is

 public function store(Request $request)
{
    $request->validate([
        'amount' => ['required', 'integer', 'max:2000000'],
        'payment_method' => ['required', 'string', 'max:255'],

    ]);

    $moneyTradeDeposit = new MoneyTradeDeposit();

    $moneyTradeDeposit->mt_dep_number = uniqid('MTDepNumber-');

    $moneyTradeDeposit->amount = $request->input('amount');
    $moneyTradeDeposit->payment_method = $request->input('payment_method');

    $moneyTradeDeposit->user_id = auth()->id();

    $moneyTradeDeposit->save();


    return redirect()->route('mt.deposit')->withMessage('Added a New Deposit');
}

What is the best way to achieve this? Thanks a lot!

1
  • Would you please clarify more. You want to sum all amount inside the foreach loop and want to get all sum (total amount from each row) outside the foreach loop? Commented Nov 3, 2020 at 12:34

2 Answers 2

1

you can either use the sum method on the collection like this:

$moneytradeDeposits->sum('amount')

or use the query builder method:

MoneyTradeDeposit::sum('amount')
Sign up to request clarification or add additional context in comments.

Comments

1

If I really get your question, then you can do like this :

@php
   $sum = 0;
@endphp
@foreach ($moneytradeDeposits as $mtd)
   <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Deposited</div>
   <div class="h5 mb-0 font-weight-bold text-gray-800">Php {{$mtd->amount}}</div>
  @php
      $sum += (float)$mtd->amount;
  @endphp
@endforeach

Now you can access $sum as total of all amount

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.