In this episode, we dive deep into customizing how Laravel renders pagination links by creating a custom pagination presenter. If you’ve ever wanted to tweak the markup and styling of the default Laravel pagination (which is geared for Bootstrap out of the box), this is exactly what you need.
We start with a simple demo setup: a posts
table with about 100 records and a basic paginated view rendering five posts per page. After wiring up a default Bootstrap-styled pagination (including pulling Bootstrap into the project), we explore how Laravel internally generates the pagination links, using presenters rather than Blade views.
Next, we walk through building a custom presenter class. This includes extending the necessary contracts, copying over important methods for handling pagination links, and deciding whether to leverage Laravel's pagination traits or write your own logic. After getting the custom presenter wired up, we see how to drop it into a custom view, giving you total control over the HTML output.
From there, we play around with changing the look and feel. Want to show every page link with no dots? You can do that! Want to change the next/previous button text or completely change the structure? You’ll see how. We even discuss splitting up the previous/next buttons from the page number links so you can float or style them separately in your markup.
By the end, you’ll understand how to roll your own pagination presenters and create different variations for your project’s needs, rather than being tied to only the stock options. If you ever craved more flexibility from Laravel’s paginator, this hands-on episode will set you up with everything you need.