I know that when using the query builder, it is possible to sort by multiple columns using
...orderBy('column1')->orderBy('column2')
but now I am dealing with a collection object. Collections have the sortBy
method, but I have not been able to figure out how to make it work for multiple columns. Intuitively, I initially tried to use the same syntax as orderBy
.
sortBy('column1')->sortBy('column2)
but this apparently just applies the sorts sequentially and it ends up sorted by column2, disregarding column1. I tried
sortBy('column1', 'column2')
but that throws the error "asort() expects parameter 2 to be long, string given". Using
sortBy('column1, column2')
doesn't throw an error, but the sort appears to be pretty random, so I don't really know what that actually does. I looked at the code for the sortBy method, but unfortunately I am having a hard time understanding how it works.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…