Group By with Sum using Laravel Collection

Today, We want to share with you Group By with Sum using Laravel Collection.In this post we will show you laravel eloquent group by relation, hear for laravel sum multiple columns we will give you demo and example for implement.In this post, we will learn about how to use sum in laravel with an example.

Group By with Sum using Laravel Collection

There are the Following The simple About laravel select count and sum Full Information With Example and source code.

Read Also:  Group By with Count using Laravel Collection

As I will cover this Post with live Working example to develop laravel get sum of column value, so the laravel add to collection is used for this example is following below.

Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.

Keywords : laravel collection group by example, laravel collection groupby two columns, laravel collection group by with sum, laravel collection group by with count, laravel collection group by date, laravel collection groupby map

Read Also:  Laravel 5.8 get current url with parameters

Laravel Collection Group By with Sum

Example

public function index()
{
    $collection = collect([
            ['id'=>1, 'name'=>'Karl Marx', 'subject' => 'Production', 'department' => 'Financial', 'salary' => 2999],
            ['id'=>2, 'name'=>'Benjamin Franklin', 'subject' => 'Purchasing', 'department' => 'Marketing', 'salary' => 1999],
            ['id'=>3, 'name'=>'Nelson Mandela', 'subject' => 'Accounting', 'department' => 'Financial', 'salary' => 3999],
            ['id'=>4, 'name'=>'Thomas Jefferson', 'subject' => 'Purchasing', 'department' => 'Marketing', 'salary' => 2999],
        ]);
  
    $grouped = $collection->groupBy('department')->map(function ($row) {
                    return $row->sum('salary');
                });
   
    dd($grouped);
}

Final Result Execute Collection Object

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [Financial] => 5999
            [Marketing] => 3999
        )
)

Web Programming Tutorials Example with Demo

Read :

Read Also:  Group By with Multiple Columns using Laravel Collection

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel firstwhere.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 6 Display Message box
  2. Read Also:  Chosen Ajax Autocomplete Textbox using jQuery, PHP
  3. Read Also:  Laravel Complex queries using Eloquent Query
  4. Read Also:  PHP - Array Functions Example With Demo
  5. Read Also:  How to count files in a directory using PHP Laravel?
  6. Read Also:  How to send mail from localhost XAMPP using PHP?
  7. Read Also:  validation Update User Profile in Laravel 5.7
  8. Read Also:  PHP Array Tutorial With Examples - filter array
  9. Read Also:  MySQL ORDER BY DESC & ASC
  10. Read Also:  jQuery Get Selected Option From Dropdown Example
CLOSEX