0

I'd like to paginate a page. I'm using this code in my controller:

$docItems = DocumentationItem::orderBy('id', 'DESC')->get()->paginate(5);

and this in my view file:

{{ $docItems->withQueryString()->links() }}

but I am getting this error:

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

I've checked similar questions here but none of the answers seemed to work for me. I'm using Laravel 7. What am I doing wrong?

flashcard
  • 29
  • 1
  • 4
  • `->get()` returns a `Collection`, which does not have a `Paginate` method. Your code should be: `DocumentationItem::orderBy('id', 'DESC')->paginate(5);` (just remove the `->get()`). – Tim Lewis Apr 05 '21 at 15:41

1 Answers1

0

you can not paginate after fetching data from db.

when you call get() method all DocumentationItem will be fetched from db.

insteadd you should use paginate without get:

$docItems = DocumentationItem::orderBy('id', 'DESC')->paginate(5);
OMR
  • 11,736
  • 5
  • 20
  • 35