Reputation: 543
I have a question. I have an user model and a post model. A user can have many posts. Now I need to get posts with pagination. And I need to sort posts by their related user's id desc. Per page number is 10. I do not know how to write this code, someone can help me? Thanks.
Upvotes: 1
Views: 34
Reputation: 163798
Since Post
belongs to User
the posts
table has user_id
key. Use it:
Post::orderBy('user_id', 'desc')->paginate(10);
Upvotes: 1