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
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!

amountinside the foreach loop and want to get all sum (total amount from each row) outside the foreach loop?