Laravel 方法分页不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48148472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:52:08  来源:igfitidea点击:

Laravel Method paginate does not exist

laravelmethodspagination

提问by Andrii H.

I am trying to paginate Model result, but I am getting "Method paginate does not exist.". Here is my code:

我正在尝试对模型结果进行分页,但收到“方法分页不存在。”。这是我的代码:

$user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10);

I need to get all records where users id equals current authenticated users id. Works well without paginate() method.

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

回答by Amarnasan

Extending a bit Alexey's perfect answer :

扩展一点阿列克谢的完美答案:

Dispatch::all()=> Returns a Collection

Dispatch::all()->where()=> Returns a Collection

Dispatch::where()=> Returns a Query

Dispatch::where()->get()=> Returns a Collection

Dispatch::where()->get()->where()=> Returns a Collection

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

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

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

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

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

You can only invoke "paginate" on a Query, not on a Collection.

您只能paginate在 aQuery上调用 " " ,而不能在 a上调用Collection

And yes, it is totally confusing to have a wherefunction for both Queriesand Collections, working as close as they do, but it is what it is.

是的,同时为and提供一个where函数是完全令人困惑的,就像它们一样接近,但它就是这样。QueriesCollections

回答by Alexey Mezenin

You need to remove all():

您需要删除all()

Dispatch::where('user_id', Auth::id())->paginate(10);

When you're using all()you get all the rows from the table and get a collection. Then you're using collection method where()(and not Query Builder method where()) and then you're trying to use paginate()method on the collection and it doesn't exist.

当您使用时,all()您会从表中获取所有行并获取一个集合。然后您正在使用集合方法where()(而不是 Query Builder 方法where()),然后您尝试paginate()在集合上使用方法,但它不存在。

回答by Mostafa Norzade

for use all recorde and pagination , you need use below code :

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

$user_dispatches = Disspath::paginate(8);

回答by Joan Nguyen

You need remove method all():

您需要删除方法all()

$user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10);

Because all()return a Collectionwhile paginate()used a Builder

因为all()返回一段Collection时间paginate()使用了Builder

回答by parastoo amini

Dispatch::where('user_id', auth()->user()->id)->paginate(10);