3

I am using laravel pagination to paginate my products By default It gives me url like this

http://example.test/search?page=3

But I want some like

http://example.test/search/page/3

OR If Possible

http://example.test/search

On every page

This Is How I am doing it now but it is not giving me any result

{{  $books->appends(Request::except('page'))->links() }}

And I have also tried like this

$books = Book::paginate(10);
$books->withPath('/search');

But still no result

xenex-media
  • 173
  • 1
  • 15
  • Does this answer your question? [Laravel pagination pretty URL](https://stackoverflow.com/questions/20974404/laravel-pagination-pretty-url) – Calos Jan 02 '20 at 06:42
  • as laravel pagination create query string ``` ?page=3 ``` like this if u want to use /page/1 then u need to create route for that and handel ur pagination manually – Kamlesh Paul Jan 02 '20 at 07:03
  • Can you explain in details ? – xenex-media Jan 02 '20 at 07:16

1 Answers1

3

You can use this to generate custom link where you use link() in your blade.php.

@php
    $links = $books->links();
    $pattern = $replacement = array();
    $pattern[] = '/'.$books->getCurrentPage().'\?page=/';
    $replacement[] = '';
    $customLinks =  preg_replace($pattern, $replacement, $links);
    echo $customLinks;
@endphp

Also you can use a package named laravel-paginateroute from spatie in github.

nayeemdev
  • 1,201
  • 1
  • 13
  • 19
  • Then see this blog [https://stillat.com/blog/2014/07/28/laravel-paginator-pretty-urls](https://stillat.com/blog/2014/07/28/laravel-paginator-pretty-urls) It will help you. – nayeemdev Jan 02 '20 at 11:01