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

t0berius's avatar
Level 13

pagination() generate relative URLs

Is there a way to make ->paginate() generate relative URLs like /users?page=3 instead of domain.tld/users?page=3.

I wasn't able to find anything related inside doc.

0 likes
4 replies
LaryAI's avatar
Level 58

To make the ->paginate() method in Laravel generate relative URLs instead of absolute URLs, you can use the Paginator::useBootstrap() method to customize the URL generation. However, Laravel's paginator by default generates absolute URLs. To achieve relative URLs, you can modify the URL generation logic by using a custom paginator view or by overriding the URL generation method.

Here's a simple way to achieve this by overriding the URL generation:

  1. Create a Custom Paginator View:

    First, you need to publish the pagination views so you can customize them:

    php artisan vendor:publish --tag=laravel-pagination
    

    This will copy the pagination views to resources/views/vendor/pagination.

  2. Modify the Pagination View:

    Open the pagination view file you want to modify, for example, resources/views/vendor/pagination/bootstrap-4.blade.php.

  3. Customize the URL Generation:

    In the pagination view, you can modify the URL generation logic. Replace the url method with a custom function that generates relative URLs. For example:

    In this example, request()->url() is used to get the current URL without the query string, and then '?page=' . $page is appended to create a relative URL.

By following these steps, you can customize the pagination links to use relative URLs in your Laravel application.

Niush's avatar
Niush
Best Answer
Level 50

Something like this in your AppServiceProvider boot() method.

use Illuminate\Pagination\Paginator;

Paginator::currentPathResolver(function () {
    return '/' . request()->path();
});

But, I am also curious why?

1 like
t0berius's avatar
Level 13

The project is using multiple domains and even generating them on the fly; server team just told me to make sure always use "relative" URLs when you reference pages of the application itself.

@niush what a great peace of code, works just fine.

Please or to participate in this conversation.