Im trying to order 'asc' the collection below but its not working with the sortBy collection method. Do you know how to properly achieve that?
$topCategories = PostCategory::pluck('category_name', 'category_icon')->toArray();
$topCategories = collect($postInformation['categories']['data'])->map(function ($postCategory) {
return
$postCategory['name']['en'];
})->sortBy('category_name')->toArray();
dd($topCategories)
shows like:
^ array:12 [▼
0 => "Testing category"
1 => "category 2"
...
]
But the category 2 should appear first.
CodePudding user response:
You can order it in the actual query:
$topCategories = PostCategory::->orderBy('category_name', DESC")->pluck('category_name', 'category_icon')->toArray();
Which is the better way, because you want need to iterate over each entity in the collection, but still if you want you can use as a function on the collection:
sortByDesc("category_name")