Laravel Method paginate不存在

And*_* H. 12 methods pagination laravel

我试图分页模型结果,但我得到"方法分页不存在.".这是我的代码:

$user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10);
Run Code Online (Sandbox Code Playgroud)

我需要获取用户id等于当前经过身份验证的用户ID的所有记录.没有paginate()方法可以很好地工作.

Ama*_*san 27

扩展一点Alexey的完美答案:

Dispatch::all() =>返回一个 Collection

Dispatch::all()->where() =>返回一个 Collection

Dispatch::where() =>返回一个 Query

Dispatch::where()->get() =>返回一个 Collection

Dispatch::where()->get()->where() =>返回一个 Collection

你只能paginate在a Query上调用" " ,而不能在a 上调用Collection.

是的,where为两者提供一个功能Queries并且Collections尽可能地接近它是完全令人困惑的,但它就是它的本质.

  • 很好地解释了。应标记为正确答案 (7认同)

Ale*_*nin 18

您需要删除all():

Dispatch::where('user_id', Auth::id())->paginate(10);
Run Code Online (Sandbox Code Playgroud)

当你使用时,all()你从表中获取所有行并获得一个集合.然后你正在使用集合方法where()(而不是查询生成器方法where()),然后你试图paginate()在集合上使用方法,它不存在.


小智 7

您需要删除方法all()

$user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10);
Run Code Online (Sandbox Code Playgroud)

因为all()Collection一会儿paginate()用了Builder


Mos*_*ade 5

为了使用所有记录和分页,您需要使用以下代码:

$user_dispatches = Disspath::paginate(8);
Run Code Online (Sandbox Code Playgroud)


小智 5

该方法toQuery()更改一个集合来查询:

$pacientes = Paciente::get()->toQuery()->paginate(20);
Run Code Online (Sandbox Code Playgroud)