This episode is for members only

Sign up to access "Laravel Performance" right now.

Get started
Already a member? Sign in to continue
06. Pagination


So this one might seem pretty obvious, but over on the homepage at the moment, we are just iterating through all of our posts, we're not actually paginating anything.
And it's really important to consider pagination, particularly when you start a project and you start iterating through stuff, but you have no idea that in the future, things might get a little bit longer than you expect. Now, luckily in Laravel, pagination is very, very easy to do.
So we're going to go over and just start to paginate this data, keeping an eye on not the query count, because it's going to be the same or roughly the same, because we might not have as many users included in that first pagination set, but also the memory usage as well. And of course, the request duration for our app.
Okay, so we're going to head over to the home controller, and we're going to switch get out to paginate. And we're going to choose a sensible amount of records that we want to display per page. Now, when we come over, what we should see here is the amount of users being pulled in reduced, we'll see the memory usage go down,
and we should see the request duration go down as well. So you can see here that we have a additional query, but that's fine. That's just part of the pagination, selecting the amount of posts that we currently have. But you can see the memory usage has gone down dramatically here.
The request duration has gone down as well. And of course, on the client side, this is going to just generally be faster in the browser, because we have less data being rendered out to the page. So of course, we need some pagination links. So let's head over to the homepage here.
And just outside of this for each, we can go ahead and say posts and links. And we're pretty much done. What we should now see is some pagination down here that we can click through and see page equals three, whatever page we've clicked on. So obviously, really, really straightforward, you may have implemented pagination before.
But the reason I bring this up is it's really easy to output a list of data with your dummy data that you're working with locally. But then when your app gets pushed to production, and you have a huge amount of data flow in like posts or comments,
you're going to end up seeing a massive delay in your page load speed, because that's something you can't control. With pagination, at least you can control what is displayed per page. So you have a very good idea of the speed of your app.
15 episodes1 hr 9 mins


Let's keep our Laravel applications feeling snappy! In this course, we cover the absolute fundamentals you need to keep in mind when building anything with Laravel.

While Laravel handles a lot for you, it's easy to fall into the trap of not considering and monitoring performance as you go. Keep these tips in your toolbelt, and you'll be able to develop faster apps, from the beginning.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.