Skip to content

Instantly share code, notes, and snippets.

@azimidev
Last active August 9, 2021 18:22
Show Gist options
  • Save azimidev/0c1a4ff1b95d5195c82f1da526425c1e to your computer and use it in GitHub Desktop.
Save azimidev/0c1a4ff1b95d5195c82f1da526425c1e to your computer and use it in GitHub Desktop.
Laravel Custom Paginate
<?php
/**
* @param $items
* @param $perPage
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
function custom_paginate($items, $perPage)
{
$pageStart = request('page', 1);
$offSet = ($pageStart * $perPage) - $perPage;
// $itemsForCurrentPage = array_slice($items, $offSet, $perPage, TRUE);
$itemsForCurrentPage = $items->slice($offSet, $perPage);
return new Illuminate\Pagination\LengthAwarePaginator(
$itemsForCurrentPage, $items->count(), $perPage,
Illuminate\Pagination\Paginator::resolveCurrentPage(),
['path' => Illuminate\Pagination\Paginator::resolveCurrentPath()]
);
}
@vimgithub
Copy link

vimgithub commented Oct 17, 2018

I tried a lot of paging and there are bugs, this is normal. Good job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment