Created
December 2, 2020 13:00
-
-
Save rizkhal/9b09dd326340998198fc438a8ed19347 to your computer and use it in GitHub Desktop.
Laravel custom paginate, you can make paginate using Builder or array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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