shymao's avatar

Pagination Failure

Hello everyone, I manage to make a pagination with every 3 results and write the following code.

$news = News::paginate(3)->sortByDesc('updated_at')->toArray(); //write in controller
{{ $news->render() }} //write in the blade

However it ends up with error and if I only type the code in first line(controller) there won't be error with the page but it can only show with 3 results. Therefore, I think it is the problem of my code on second line! Can anyone give me some suggestion? Thanks for reading.

0 likes
6 replies
tisuchi's avatar

Because you are trying to do pagination on array.

Just try like so-

$news = News::sortByDesc('updated_at')->paginate(3); //write in controller
{{ $news->render() }} //write in the blade

Ref: https://laravel.com/docs/5.4/pagination

2 likes
shymao's avatar

@tisuchi @willvincent But it still ends up with error. And I have try to debug with following.....

$news = News::sortByDesc('updated_at')->paginate(3); //write in controller
dd($news);

I even cannot get the content of $news.

willvincent's avatar

that should also be orderBy.. or latest

Try this:

$news = News::latest('updated_at')->paginate(3);

and {{ $news->links() }} in your view

sortBy is a helper for collections, orderBy is a query builder directive.

1 like

Please or to participate in this conversation.