2017-07-18 112 views
0

我想要一個集合,將它按1個字段分組,並按多個字段排序。Laravel group by 1 order by multiple

這件事情我已經試過:

$permission_groups = collect(Permission::orderBy('group', 'asc')->orderBy('name', 'asc')->get()); 
    $permission_groups->groupBy('group'); 

原因Trying to get property of non-object

$permission_groups = Permission::all()->sortBy('group', 'asc')->sortBy('name', 'asc')->groupBy('group'); 

導致對集合進行排序,然後使出。

我該如何簡單地按2列進行排序並對其進行分組?

回答

0

想通了:

$permission_groups = Permission::orderBy('group', 'asc')->orderBy('name', 'asc')->get()->groupBy('group');