Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing query string from laravel url in pagination

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

like image 277
xenex-media Avatar asked Sep 02 '25 09:09

xenex-media


1 Answers

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.

like image 101
nayeemdev Avatar answered Sep 04 '25 23:09

nayeemdev