What is the syntax for sorting an Eloquent collection by multiple columns?

前端 未结 4 1956

I know that when using the query builder, it is possible to sort by multiple columns using

...orderBy(\'column1\')->orderBy(\'column2\')

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 06:01

    A simple solution is to chain sortBy() multiple times in reverse order of how you want them sorted. Downside is this is likely to be slower than sorting at once in the same callback, so use at your own risk on large collections.

    $collection->sortBy('column3')->sortBy('column2')->sortBy('column1');
    

提交回复
热议问题