Laravel Collection Sum Column Example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello dev’s . Hope are you good.Today on this tutorial, i will like to show you laravel collection sum on column. I hope you can understand a concept of laravel eloquent collection sum. So now I’m going to show you about laravel eloquent sum of column query. So on this article goes in details on laravel eloquent get sum of column value group by. Now so, let’s start and follow few step to create a short example of Laravel Collection Sum Column.

So let’s see a simple examples of sum() with collection and eloquent in laravel application. Now you can easily use it with your any version on laravel application such as laravel 5, laravel 6, laravel 7 and laravel 8 applications. So let’s start and let’s see bellow examples that will help you lot on your application.

Example 1

<?php
  
namespace App\Http\Controllers;
  
use App\Models\Product;
  
class SignaturePadController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $sum = Product::sum('price');
          
        dd($sum);
    }
}

Read Also : Javascript – Convert Array into Comma Separated String Example

Output:

3456

Example 2

<?php
  
namespace App\Http\Controllers;
  
use App\Models\Product;
  
class SignaturePadController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $products = Product::select("id", "name", \DB::raw("SUM(price) as total"))
                    ->groupBy("category_id")
                    ->get();
          
        dd($products);
    }
}

Output:

Array

(

    [0] => Array

        (

            [id] => 1

            [name] => Laravel

            [total] => 24971

        )

    [1] => Array

        (

            [id] => 3

            [name] => Apple

            [total] => 3302

        )

    [2] => Array

        (

            [id] => 5

            [name] => hp

            [total] => 4190

        )

)

Example 3

<?php
  
namespace App\Http\Controllers;
  
class SignaturePadController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $sum = collect([10, 2, 8, 4, 7, 6])->sum();
          
        dd($sum);
    }
}

Read Also : How to check array is empty or null or undefined in javascript?

Output:

37

Example 4

<?php
  
namespace App\Http\Controllers;
  
class SignaturePadController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $collection = collect([
            ['name' => 'Laravel', 'price' => 176],
            ['name' => 'PHP', 'price' => 1096],
            ['name' => 'Angular', 'price' => 63],
        ]);

        $sum = $collection->sum('price');
          
        dd($sum);
    }
}

Read Also: Laravel Eloquent Sum Multiple Columns Example

Output :

1335

Thank for read me.I hope it will help you,For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →