Laravel 5.8 – From The Ground Up
Lesson 43 of 52 · Laravel 5.8 Tutorial From Scratch – e43 – Pagination · Education
Laravel 5.8 Tutorial From Scratch – e43 – Pagination
when you start to use a framework like laravel it facilitates your development tremendously and today we're going to touch up on one of those topics that is
extremely common in a lot of applications and that is the concept of pagination just so that we are all on the same page pagination simply means that you're able to break up your
records into a set number and then you can divide that into different pages so in our case our customers list has 51 entries right now what if I wanted to paginate them to maybe only display 15
of those at a time and then at the bottom I want to add that next button and that way I know when to go to the next page laravel pretty much makes this a complete non-issue it is so simple to
do it's almost laughable so let's dive right in and take a look how to do pagination so let's jump into phpstorm and let me jump to my customers controller now we are obviously in the
index method because this is index method in a restful controller we are fetching all of our customers and we are eager loading our company with it and we're saving that to our customers
variable that customers variable gets passed into the view and from there we display all of our customers so let's go to the customers index view and we see here we're running a for each loop for
each of the customers we are simply just diving in and grabbing each one alright fair enough so I said I wanted to paginate this by 15 so instead of get I will actually call paginate and inside
the parentheses I'm gonna pass in 15 meaning I want 15 records go back to the browser and hit refresh and right away you see that we are actually paginating however we have no way right now of
being able to display the next page we're stuck on page 1 all right let's go back to our index and now right down here right outside of the for each I'm gonna actually display the links let me
do a new row with a column of 12 and I do want that to also be text Center inside of here I'm going to echo out using our blade syntax and all I have to do is called customers and then
call this links method the links method is what label is going to output out as your links for the bottom let's go back hit refresh and there we are so now we have pagination second page third page
fourth page you can even navigate using the arrows like I said level makes pagination pretty much a non-issue all the way around now it looks like it didn't like my text center so let's go
ahead and fix that now instead of sex center I'm just gonna say give me a display of flex and less justifies content Center hit refresh and there we are let's add a little bit of padding
top maybe padding top five there we go so that's it we are paginating it's that simple level just makes this so simple it's a complete non-issue and now you can
simply paginate any customers list it doesn't matter how large it is as a matter of fact let's add another 500 customers to this list we can simply do that using our factory I'm gonna call my
factory and I'm gonna say 500 it's gonna take a little bit here and there we are let's go back hit refresh and sure enough now we have 37 pages worth of customers and it's all perfect it all
works even though we have a lot of customers so that's great news now if we wanted to display more than 15 out of time it is just as simple as
going back to your customers controller in this 15 let's say I want to display 25 at a time now let's go back hit refresh and now we get 25 at a time so there it is 26 through 50 and so on and
so forth so that's it that's pagination in levo go ahead and implement pagination in your own project if you've been following along and when you're ready I'll see you in the next lesson
Pagination is the simple concept of splitting up records into a set number and then displaying them in different pages. Laravel makes this so easy to do, it’s almost laughable. Follow along as we paginate our customer’s list.
For the best experience, follow along in our interactive school at https://www.coderstape.com
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!