shAkur
shAkur

Reputation: 1002

Laravel 5 pagination links

I have a laravel 5 application in which I`m trying to do a pagination for certain results (array of results not collection). This is my code:

$paginator = new LengthAwarePaginator(
 $l_aResponse['body'],
 count($l_aResponse['body']),
 '2',
 Paginator::resolveCurrentPage(),
 ['path' => Paginator::resolveCurrentPath()]
);
return str_replace('/?', '?', $paginator->render());

My question is: is there a way to modify the way in which the "page" parameter is setup in the URL for pages? EG: I don't want this format: http://localsite/articles?page=3 but I want http://localsite/articles/3

I`ll appreciate any answer. Thanks!

Upvotes: 1

Views: 1967

Answers (1)

Thomas Van der Veen
Thomas Van der Veen

Reputation: 3226

The __construct() function of the LengthAwarePaginator class has a few parameters:

  • $items
  • $total
  • $perPage
  • $currentPage = null
  • array $options = []

As you can see the fourth parameter represents the current page.

So you can use a custom desired current page which has been retrieved from the last parameter from the url. An example would be:

//  http://localsite/articles/3

$currentPage = ... // Get parameter from url

$paginator = new LengthAwarePaginator(
    $l_aResponse['body'],
    count($l_aResponse['body']),
    '2',
    $currentPage, // Current page as fourth parameter
    ['path' => Paginator::resolveCurrentPath()]
);

return str_replace('/?', '?', $paginator->render());

Have not tested this. Hope it helps :)

Upvotes: 3

Related Questions