Skip to content

Instantly share code, notes, and snippets.

@rizkhal
Created December 2, 2020 13:00
Show Gist options
  • Save rizkhal/9b09dd326340998198fc438a8ed19347 to your computer and use it in GitHub Desktop.
Save rizkhal/9b09dd326340998198fc438a8ed19347 to your computer and use it in GitHub Desktop.
Laravel custom paginate, you can make paginate using Builder or array
<?php
namespace App\Utils;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class CustomPaginate
{
/**
* The attributes that are mass assignable.
* This method for make pagination from Builder
*
* @var array
*/
public function paginate($items, $perPage = 5, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment