skip to Main Content

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.

2

Answers


  1. You can do the following:

    $total = 0;
    $amounts = Collections::all()->pluck('amount');
    foreach($amounts as $amount){
      $total += $amount;
    }
    
    Login or Signup to reply.
  2. You can use sum from eloquent:

    $amount = Collections::sum('amount');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search