Belal Khan
Belal Khan

Reputation: 148

After group data in laravel how to show all rows from each group separately

I am using groupBy in laravel to make group, but after grouping I want to show all rows in separate array which are in the same group.

$menus = Order_menu_mapping::whereIn('order_id', $order_ids)
    ->groupBy('menu_id')
    ->get();

Here $menus just make group which keeps only one row. But I want to show all row inside of each group.

Upvotes: 1

Views: 1054

Answers (2)

Hamid Vetr
Hamid Vetr

Reputation: 111

you need to use mapToGroups . that groupBy works on the data set

Upvotes: 0

Rwd
Rwd

Reputation: 35180

In your example you're adding groupBy to the query which is not the same as using groupBy() on a collection.

The get() method will perform the query and return a collection so you just need to put the groupBy after it:

$menus = Order_menu_mapping::whereIn('order_id', $order_ids)
    ->get()
    ->groupBy('menu_id');

Upvotes: 4

Related Questions