I made this service for my paginations :
<?php namespace App\Services;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
abstract class Pagination
{
/**
* Create paginator
*
* @param Illuminate\Support\Collection $collection
* @param int $total
* @param int $perPage
* @return string
*/
public static function makeLengthAware($collection, $total, $perPage, $appends = null)
{
$paginator = new LengthAwarePaginator(
$collection,
$total,
$perPage,
Paginator::resolveCurrentPage(),
['path' => Paginator::resolveCurrentPath()]
);
if($appends) $paginator->appends($appends);
return str_replace('/?', '?', $paginator->render());
}
}
You create links with :
$links = Pagination::makeLengthAware(Pagined Eloquent Collection, Total Count, Count per page);
You can append parameters with fourth parameter (for search it's nice).