简体   繁体   中英

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?

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

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

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

Or:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM