Group By Eloquent ORM


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


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

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

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


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: