Limit amount of links shown with Laravel pagination

Limit amount of links shown with Laravel pagination

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Limiting Laravel Pagination Links for Mobile Optimization

By default, Laravel's pagination helper attempts to render a comprehensive list of page links, often resulting in a sequence like 1 2 3 4 5 7 8 .. 78 79. While this provides excellent navigation on desktop screens with large viewports, it creates significant usability issues on mobile devices. The sheer volume of links can force the navigation bar to wrap onto a second line or consume excessive vertical space, degrading the user experience for touch interfaces.

The core mechanism driving this behavior is the LengthAwarePaginator class. When you call paginate(), Laravel constructs an instance that knows both the total number of items and the current page. The default presentation logic attempts to show a reasonable spread of pages around the current one, but it doesn't strictly enforce a hard maximum count of visible links by default in older versions or without specific configuration.

To address this, you need to intervene in how the paginator renders its HTML. The most direct approach involves customizing the LengthAwarePaginator class itself. You can create a new class that extends the default paginator and override the render() method, which is responsible for generating the view. Inside this overridden method, you can manipulate the $collection of links before they are passed to the Blade template or the underlying CSS framework.

Here is a practical implementation strategy using a custom paginator class:

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

class LimitedLinksPaginator extends LengthAwarePaginator
{
    public function __construct(array $items, array $collection, int $perPage, int $currentPage, ?string $pageName = null)
    {
        parent::__construct($items, $collection, $perPage, $currentPage, $pageName);
    }

    public function render()
    {
        // You can customize the links collection here before rendering.
        // This example assumes you want to limit the total number of links shown.
        
        // Note: The actual logic depends heavily on your blade template 
        // or CSS framework (like Tailwind or Bootstrap) used for display.
        
        return parent::render();
    }
}

However, simply extending the class isn't enough if you aren't updating the view that consumes the data. The real power lies in the Blade template where the pagination links are displayed. If you are using a custom view, you can manually filter the $links array provided by the paginator. For example, you might iterate through the links and skip those beyond a certain index count, effectively truncating the list to show only the first 10 items or a specific range.

A more robust solution often involves checking the total number of pages before rendering the full list. If the total page count is high, you can dynamically decide whether to render a truncated list with ellipses or stick to a fixed window around the current page. This logic prevents the "13 links at most" issue by enforcing a stricter constraint on the output.

If you are using a CSS framework like Tailwind CSS, you can leverage utility classes to handle the overflow visually. By setting overflow-x-auto on the pagination container, you allow users to scroll horizontally if they choose to see more pages, while keeping the primary navigation compact. This approach offers a graceful degradation: it keeps the UI clean on mobile but doesn't hide functionality from desktop users who might want to jump directly to page 78.

It is important to note that modifying the default resources/views/vendor/pagination files in Laravel can be overwritten during upgrades. For production environments, wrapping the paginator in a custom class or creating a dedicated layout component is safer than altering vendor views directly. This ensures your link-limiting logic persists across updates without breaking.

Ultimately, the goal is balancing information density with screen real estate. By controlling the number of visible links, you ensure that mobile users can navigate quickly without scrolling through a cluttered list of page numbers.

Our network