Laravel 分页中不存在方法链接

Method links do not exist on Laravel pagination

我正在尝试实现分页,在我添加 sortByDesc() 以及我的 eloquent 查询之前,它工作得非常好。

web.php(路由文件)

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

当我在指定视图中使用 {{ $posts->links() }} 准备分页视图时,出现以下错误 -

Method links does not exist

如果我从查询中删除排序条件,它将完美运行。

这种行为背后的原因是什么?

尝试对查询进行排序而不是分页:

Post::orderBy('post_id', 'desc')->simplePaginate(5);

延伸到@RossWilson 所说的内容。

sortBy是集合函数,不是eloquent函数,正确的eloquent函数是orderBy.

另外,看 simplePaginate() 就好像你在表演 get(), first(), find()

你会先买什么还是先下单? ...如果你想订购一个集合(使用 sortBy),也许是 get,但是因为 simplePaginate 不 return 与 get() 会 [=29] 相同的集合=], sortby 不起作用。并且可能搞乱了分页 object/collection。