问题
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 ?
回答1:
Eloquent uses the query builder internally, so you can do:
$users = User::orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 100)
->get();
回答2:
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();
来源:https://stackoverflow.com/questions/22562101/group-by-eloquent-orm