How to order a relationship in Laravel?

I saw this question in StackOverflow, but I could not answer it because it was mistakenly marked as a duplicate. However, $ user-> posts does not match Posts :: all (). So, if you have User and Post, how do you sort the messages?

+1
orm laravel-5
Oct 24 '15 at 17:54
source share
1 answer

I, answering my question, just to help.

class User extends Model implements ... { public function posts(){ return $this->hasMany('App\Post'); } public function recentPosts($limit = 5){ return $this->posts ->sortByDesc(function($post){ return $post->created_at; })->take($limit); } } 
0
Oct 24 '15 at 17:54
source share



All Articles