8

I'm new to Laravel and thought it be cool to purchase the Codehappy ebook by Dayle Rees.

I just finished the blog tutorial and thought a bit on how he retrieved the posts from the Post model. Coming from a .net (ASP.NET MVC) background I think it will be important to order the posts while eager loading the author.

He eager loads the model like this.

$posts = Post::with('author')->get();

My question is where can you use the "order_by" clause? the order_by itself works when I use:

$posts = Post::order_by('id', 'desc')->get();

Regards RaVen

Laurence
  • 58,936
  • 21
  • 171
  • 212
RaVen
  • 775
  • 2
  • 8
  • 21

1 Answers1

8

I manage to solve it by:

$posts = Post::with('author')->order_by('id', 'desc')->get();
RaVen
  • 775
  • 2
  • 8
  • 21
  • Indeed this is the correct answer. Eloquent extends the Fluent query builder so it inherits functions like where() and order_by(). Eloquent introduces the "eager loading" feature, but is required to be the first part of the method chaining (ie the static method called first) – Joel Larson Jan 07 '13 at 12:22
  • Laravel on Stackexchange http://area51.stackexchange.com/proposals/46607/laravel?referrer=VUgOWgZpXyO753uZWv1VMg2 – Brian Dillingham Apr 08 '13 at 21:53