4

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

3
  • Does this answer your question? Laravel pagination pretty URL Commented Jan 2, 2020 at 6: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 Commented Jan 2, 2020 at 7:03
  • Can you explain in details ? Commented Jan 2, 2020 at 7:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.