Group By Eloquent ORM

前端 未结 3 1363
天涯浪人
天涯浪人 2020-12-10 00:47

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 sh

相关标签:
3条回答
  • 2020-12-10 01:23

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

    $users = User::orderBy('name', 'desc')
                    ->groupBy('count')
                    ->having('count', '>', 100)
                    ->get();
    
    0 讨论(0)
  • 2020-12-10 01:33

    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();
    
    0 讨论(0)
  • 2020-12-10 01:40

    try: ->unique('column')

    example:

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

    0 讨论(0)
提交回复
热议问题