Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sauravs012's avatar

How to make perfect URL using paginate in Laravel?

My Controller

$posts = Posts::where('status','publish')->orderBy('id', 'desc')->take(10)->get();
return view('front', compact('posts'));

My View

{{$posts->links}}

I want convert

http://127.0.0.1:8000/?page=3

TO

http://127.0.0.1:8000/page/3

Thanks

0 likes
11 replies
jlrdw's avatar

Did you study the code used in the spatie library to get some ideas.

MichalOravec's avatar

Do you mean pagination links? In the same way like in Laravel, with custom pagination view.

https://laravel.com/docs/8.x/pagination#customizing-the-pagination-view

{{ $items->links('custom.pagination') }}

For example views/custom/pagination.blade.php

@if ($paginator->hasPages())
    <nav class="text-center">
        <ul class="pagination pagination-md">
            {{-- Previous Page Link --}}
            @if ($paginator->onFirstPage())
                <li class="disabled"><span>@lang('pagination.previous')</span></li>
            @else
                <li><a href="{{ PaginateRoute::previousPageUrl() }}" rel="prev">@lang('pagination.previous')</a></li>
            @endif

            {{-- Pagination Elements --}}
            @foreach ($elements as $element)
                {{-- "Three Dots" Separator --}}
                @if (is_string($element))
                    <li class="disabled"><span>{{ $element }}</span></li>
                @endif

                {{-- Array Of Links --}}
                @if (is_array($element))
                    @foreach ($element as $page => $url)
                        @if ($page == $paginator->currentPage())
                            <li class="active"><span>{{ $page }}</span></li>
                        @else
                            @if (in_array($paginator->currentPage(), [$page - 1, $page, $page + 1]))
                                <li><a href="{{ PaginateRoute::pageUrl($page) }}">{{ $page }}</a></li>
                            @else
                                <li class="hidden-xs"><a href="{{ PaginateRoute::pageUrl($page) }}">{{ $page }}</a></li>
                            @endif
                        @endif
                    @endforeach
                @endif
            @endforeach

            {{-- Next Page Link --}}
            @if ($paginator->hasMorePages())
                <li><a href="{{ PaginateRoute::nextPageUrl($paginator) }}" rel="next">@lang('pagination.next')</a></li>
            @else
                <li class="disabled"><span>@lang('pagination.next')</span></li>
            @endif
        </ul>
    </nav>
@endif
VTrace's avatar

@MichalOravec Hi, I got Undefined variable $elements when I use custom pagination blade like you did. Is there any solution?

VTrace's avatar

@MichalOravec Thread posted, sorry for tagging you, although I can't provide any links since this is my first day joining this forum.

Please or to participate in this conversation.