Group By Eloquent ORM

26

I was searching for making a GROUP BY name Eloquent ORM docs but I haven't find anything, neither on google.

Does anyone know if it's possible ? or should I use query builder ?

This question is tagged with laravel group-by laravel-4 eloquent

~ Asked on 2014-03-21 14:52:42

The Best Answer is


46

Eloquent uses the query builder internally, so you can do:

$users = User::orderBy('name', 'desc')
                ->groupBy('count')
                ->having('count', '>', 100)
                ->get();

~ Answered on 2014-03-21 14:58:10


8

Laravel 5

This is working for me (i use laravel 5.6).

$collection = MyModel::all()->groupBy('column');

If you want to convert the collection to plain php array, you can use toArray()

$array = MyModel::all()->groupBy('column')->toArray();

~ Answered on 2018-08-17 10:51:56


Most Viewed Questions: