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_
Dispatch::where('user_id', auth()->user()->id)->paginate(10);
You need to remove 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.
for use all recorde and pagination , you need use below code :
$user_dispatches = Disspath::paginate(8);
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
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.
You need remove method all()
:
$user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10);
Because all()
return a Collection
while paginate()
used a Builder