Get your collection from db. All 200 records. Then create an instance of Paginator class. (for laravel 5) Like this :
$paginator = new Paginator($items->forPage($page,$per_page),$items->count(),$per_page,$page);
where $items is your collection
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Suppose I have 200 records in my DB. While displaying those records I want to paginate them. The normal approach would be something like
$tasks = Tasks::paginate(10); Then $tasks->links();.
That code works fine. However, With that code you gonna run DB query every time you fetch 10rows (200/10=20times).
What I need is to fetch all data at once, store them in a collection (say $tasks),Then paginate the resulting collection instead of fetching again from DB.
Even more, I need to query something like "Get only 100 records and paginate them by 10 " which would be nice if it would be something like Tasks::take(100)->paginate(10) but unfortunately that even do not work.
Anyone with an idea on how to tackle these issues?
Please or to participate in this conversation.