-2

I need to sort posts by highest id :/. Currently, the code sorts them by the lowest ID.

$posts->sortBy('id');

How can I do that?

Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88
  • 2
    Possible duplicate of [Laravel Eloquent: Ordering results of all()](https://stackoverflow.com/questions/17429427/laravel-eloquent-ordering-results-of-all) – Zakaria Acharki Sep 13 '18 at 14:20

2 Answers2

5
$posts->sortByDesc('id');

Source: Laravel Collections

Sand Of Vega
  • 1,938
  • 14
  • 23
0

For sorting by highest id use orderBy in Laravel, for example if you want to sort all your projects:

$projects = Project::orderBy('id', 'desc')->get();