user9266291
user9266291

Reputation:

Paginate in relationship and order by

I have List. List has many posts.

I want to get all post of some lists, sort posts and paginate it.

I try this:

$list = List::whereId('1')->firstOrFail();
$posts = $feed->posts()->orderBy('id')->paginate(Request::input('per_page'))->appends(Request::input());

return compact('list','posts');

Now I can get List and paginate posts, but ordeBy doesn't work? How order it? Or any other way to solve this problem?

Upvotes: 3

Views: 62

Answers (3)

Ali Özen
Ali Özen

Reputation: 1609

$posts= Post::orderBy('id','desc');

Upvotes: 2

Sakul Budhathoki
Sakul Budhathoki

Reputation: 366

->orderBy('id','asc') for Ascending

->orderBy('id','desc') for Decending

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

It works and sorts by ASC by default. If you want to reverse it, use DESC:

->orderBy('id', 'desc')

Or:

->latest('id')

Upvotes: 0

Related Questions