Laravel 4: how to "order by" using Eloquent ORM

Josh picture Josh · Jul 9, 2013 · Viewed 422.7k times · Source

Simple question - how do I order by 'id' descending in Laravel 4.

The relevant part of my controller looks like this:

$posts = $this->post->all()

As I understand you use this line:

->orderBy('id', 'DESC');

But how does that fit in with my above code?

Answer

Chris G picture Chris G · Aug 17, 2013

If you are using post as a model (without dependency injection), you can also do:

$posts = Post::orderBy('id', 'DESC')->get();