-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
  • 66,747
  • 15
  • 75
  • 101
  • 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
  • 2,297
  • 16
  • 29
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();
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38