Home > Software engineering >  how to loop through table in laravel calculate sum of a column
how to loop through table in laravel calculate sum of a column

Time:10-18

How do i calculate the totals of all values of a column amount after fetching it in laravel controller. table collections has only amount and id column and i want to determine the totals.

CodePudding user response:

You can use sum from eloquent:

$amount = Collections::sum('amount');

CodePudding user response:

You can do the following:

$total = 0;
$amounts = Collections::all()->pluck('amount');
foreach($amounts as $amount){
  $total  = $amount;
}
  • Related