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
Laravel Method paginate does not exist
提问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 aCollection
Dispatch::all()->where()
=> Returns aCollection
Dispatch::where()
=> Returns aQuery
Dispatch::where()->get()
=> Returns aCollection
Dispatch::where()->get()->where()
=> Returns aCollection
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 where
function for both Queries
and Collections
, working as close as they do, but it is what it is.
是的,同时为and提供一个where
函数是完全令人困惑的,就像它们一样接近,但它就是这样。Queries
Collections
回答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 Collection
while paginate()
used a Builder
因为all()
返回一段Collection
时间paginate()
使用了Builder
回答by parastoo amini
Dispatch::where('user_id', auth()->user()->id)->paginate(10);