I noticed that the sortBy (orderBy) methods are a little bit different on Collection than on Query Builder, I'm not sure why that is. It seems more difficult to sort a collection by a key since it does not accept a second argument for asc/desc.
I would love to be able to sort by collection like this but I can't.
$order = 'desc';
$sorted = $collection->sortBy($company->user->id, $order)
But instead I have to do it this way. (As far as I know).
$sorted = $collection ->sortBy(function ($item) {
return $item->company->user->id;
})
So... is it possible to sort like with the first (much shorter) method?
And is it possible to specify the order of 'asc/desc' without doing a if/else like this...
if ($order == 'asc') {
$sorted = $collection ->sortBy(function ($item) {
return $item->company->user->id;
})
} else if ($order == 'desc') {
$sorted = $collection ->sortByDesc(function ($item) {
return $item->company->user->id;
})
}