dou70260 2018-08-22 07:47
浏览 90
已采纳

Laravel分页上不存在方法链接

I'm trying to achieve pagination and it is working absolutely fine until I add sortByDesc() along with my eloquent query.

web.php (route file)

Route::get('/', function(){
  $posts = Post::simplePaginate(5)->sortByDesc("post_id"); 
  //sortByDesc("post_id") this causes the problem
}

When I prepare the view for the pagination with {{ $posts->links() }} in the specified view, I get the following error-

Method links does not exist

If I remove the sorting condition from the query, it works perfectly.

What can be the reason behind this behaviour?

  • 写回答

2条回答 默认 最新

  • dongyiyu882684 2018-08-22 07:50
    关注

    Try putting the sort on the query rather than the pagination:

    Post::orderBy('post_id', 'desc')->simplePaginate(5);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?