Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Method paginate does not exist

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.

like image 849
Andrii H. Avatar asked Jan 08 '18 10:01

Andrii H.


1 Answers

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

You can only invoke "paginate" on a Query, not on 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.

like image 127
Amarnasan Avatar answered Sep 27 '22 20:09

Amarnasan