0Day Forums
Group By Eloquent ORM - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: FrameWork (https://0day.red/Forum-FrameWork)
+---- Forum: Laravel (https://0day.red/Forum-Laravel)
+---- Thread: Group By Eloquent ORM (/Thread-Group-By-Eloquent-ORM)



Group By Eloquent ORM - merylqqhcpdizz - 08-06-2023

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

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


[1]:

[To see links please register here]




RE: Group By Eloquent ORM - bargellos247810 - 08-06-2023

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

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


RE: Group By Eloquent ORM - valise922023 - 08-06-2023

try: ```->unique('column') ```

example:

``` $users = User::get()->unique('column');```


RE: Group By Eloquent ORM - profession568 - 08-06-2023

# Laravel 5

> **WARNING**: As @Usama stated in comments section, this groups by AFTER fetching data from the database. The grouping is not done by the database server.
_I wouldn't recommend this solution for large data set._


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();