I have a Collection
of Eloquent and would like to sort it by two fields at the same time being one of them a predefined value.
Ex: this Collection
has several objects of type Categoria
and I want to sort it so that categories with property slug
equal "solicitacoes"
are at the beginning and I want the rest to be in alphabetical order.
I know that the sort
method can get a callback to sort the collection and tried to do (among other things) this
$sorted = $categorias->sort(function ($item, $next) {
return $item->slug === 'solicitacoes' ? -1 :
strcmp($item->descricao, $next->descricao);
});
But the ordering worked very well, disregarding the categories with slug = "solicitacoes"
was in alphabetical order, the problem is that the aforementioned were not in the beginning.