In this episode, we dive into how to paginate our data with Eloquent. We start by swapping out the usual get()
method for paginate()
, and we talk through what Eloquent's pagination gives us out of the box—like being able to specify how many records you want per page.
You'll see what happens behind the scenes when using paginate()
(hint: it's a length-aware paginator that knows how many total items you have), and we inspect the paginator output to understand its structure. We face a small roadblock where, even though the paginator is ready to go, it doesn't know what page we're on. That means clicking different pages in the query string doesn't do anything yet!
To fix this, we install the pagination package and then head over to our application's service provider to do some essential config: setting up the path, query string, and current page resolvers. You'll see how to clean up the current path (removing the query string, so pagination links stay clean) and properly determine which page should be displayed.
By the end, pagination is working in our app—changing the page in the URL actually works! But there are no navigation links yet. That's the perfect setup for next time, where we'll get into making custom pagination templates using Twig.
So after this episode, you’ll know how to paginate data, make sure your app understands which page it should show, and you'll be prepped to customize the UI of your pagination controls next!