Laravel Collection Groupby Method Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now on this article, I will share with you a example of the how we can use Laravel collection in groupBy method. The groupBy method is used for group a collection based on a given value or represented by the $groupBy parameter. So it an argument will passed to $groupBy can be also a simple string, and it also representing the value that the collection so it should be grouped by, or can be a callback that will returns the value the collection should be on grouped by.

So the groupBy method does not preserve any keys of the collection when the grouping, but also that will be changed by the passing true as the argument to the $preserveKeys parameter. Now here the groupBy will returns a new instance of the Collection class.

So now for the following collection will be used throughout this section then when we demonstrating the groupBy method:

Example 1

For this groupBy method groups the collection’s items by a given key:

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(){

   $collection = collect([
    ['user_id' => '1', 'name' => 'xyz','impression'=>'10'],
    ['user_id' => '1', 'name' => 'adc','impression'=>'65'],
    ['user_id' => '2', 'name' => 'pdc','impression'=>'30'],
    ]);

    $grouped = $collection->groupBy('user_id');
    $grouped = $grouped->toarray();
    dd($grouped);
}

output

Output of the above $grouped variable will now have a value similar to the following output:

array:2 [?
  1 => array:2 [?
    0 => array:3 [?
      "user_id" => "1"
      "name" => "xyz"
      "impression" => "10"
    ]
    1 => array:3 [?
      "user_id" => "1"
      "name" => "adc"
      "impression" => "65"
    ]
  ]
  2 => array:1 [?
    0 => array:3 [?
      "user_id" => "2"
      "name" => "pdc"
      "impression" => "30"
    ]
  ]
]

Example 2

For the Instead of passing a string key, you may pass a callback. And the callback should return the value you wish to key the group by

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(){

   $collection = collect([
    ['user_id' => '1', 'name' => 'xyz','impression'=>'10'],
    ['user_id' => '1', 'name' => 'adc','impression'=>'65'],
    ['user_id' => '2', 'name' => 'pdc','impression'=>'30'],
    ]);
   
    $grouped = $collection->groupBy(function ($item, $key) {
        return substr($item['name'], -2);
    });
    $grouped = $grouped->toarray();
    dd($grouped);
}

output

The above $grouped variable will now have a value similar to the following output:

array:2 [▼
  "yz" => array:1 [▼
    0 => array:3 [▼
      "user_id" => "1"
      "name" => "xyz"
      "impression" => "10"
    ]
  ]
  "dc" => array:2 [▼
    0 => array:3 [▼
      "user_id" => "1"
      "name" => "adc"
      "impression" => "65"
    ]
    1 => array:3 [▼
      "user_id" => "2"
      "name" => "pdc"
      "impression" => "30"
    ]
  ]
]

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also 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 →