This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
49. Paginating with Eloquent

Episodes

0%
Your progress
  • Total: 4h 32m
  • Played: 0m
  • Remaining: 4h 32m
Join or sign in to track your progress

Transcript

00:00
Eloquent, which we're using to access our database, comes pre-built with pagination. So let's look at how we invoke this to get back just the data we want
00:09
to see on the page that we're on. And then we'll start to configure this. In the next episode, we're going to build up some custom pagination views so we can design these and have
00:21
them directly within our Twig templates. OK, so the first thing that we're going to do is head over to the home controller where we are outputting all of these users.
00:32
Now, regardless of how you're building this query up, for example, if you're using any where clauses or anything else like that, it doesn't matter. At the end of this, you just need
00:41
to swap out get for paginate. And the only argument this requires is how many you want per page. So we're going to go ahead and say
00:50
that we want one per page because we've got two records. So let's head over and give this a refresh. And the first issue that we have is although Eloquent uses this paginate method
01:01
and it's very closely tied to it, we don't have the pagination package installed. So again, we're going to come over and do a composer require here on illuminate and you guessed it, pagination.
01:15
So once we've got this installed, we will be able to see this on the page. Let's go over, give this a refresh. And as you can see, we are now only
01:24
seeing one record per page. So let's just dive into what's happening behind the scenes here when we actually use paginate. What are we getting back from this?
01:34
Well, let's go to the top here, dump this out, kill the page, and see what this gives us. OK, so I'm going to give the page a refresh. And as you can see, we've got a length aware paginator.
01:45
Length aware just means it's aware of how many records are in the database so it can paginate accordingly. And you'll see that inside of here, we have a collection. So the items within this paginator are a collection.
01:58
The paginator is iterable, so we can still iterate over this in our view. But the only items that we have in here are just on the first page.
02:06
So at the moment, we have Alex. Now, sure enough, if we go over to the query string and type page 2, what do we think is going to happen? Well, not much.
02:15
We're still going to get the first result here because our paginator does not know where to take this data from in order to select this in the database for page 2.
02:27
So what we need to do is a little bit of configuring to get this to work. What we can do now, though, is just get rid of this dump because whichever page we select on here,
02:36
we will see the data for that down here. OK, so we can configure this anywhere, but we're going to do this over in our app service provider. So you can either create out a separate service
02:46
provider for this and put this in your boot method, or you could just put it in app service provider. So there's quite a few things that we can customize on the paginator instance.
02:56
Let's go ahead and open that up here. We've got things like the current path resolver. If we just open up this base class here, you'll see that we've got a current path
03:05
resolver, a current page resolver, which needs to feed into the paginator what page we're currently on. There's a ton that we can configure here. Let's just configure the most basic ones now,
03:17
and then we'll tidy this up in a bit. So let's go ahead and grab out the paginator from illuminate and pagination. And let's set the current path resolver.
03:27
So the current path is just going to be the path that exists within our application. So basically, just the following. Or if we're within a sub page, for example, a user's page,
03:38
it will be this. So we just need to return the current path that we're on. OK, we get an error here, of course, because we haven't filled this in with a closure.
03:45
But let's go ahead and do that now. OK, so how are we going to do this? Well, we can return from our request, which, remember, is on our container.
03:52
So we can say this, get container, and get our request. That gives us our global request, remember, that we built up a little bit earlier. If we need reminding about this, let's
04:04
go over to our request service provider. And remember that we created the server object from globals. So this will contain everything about our path and everything that we need.
04:16
So on this request object, we have a get URI method, which returns to us the current URI that we are on, including the forward slash path. Unfortunately, it also includes anything in the query string.
04:30
So if we head back over to the browser, give this a refresh, that looks like it works. But we're going to end up with too much data in here. So let's just go ahead and dump this out in here
04:41
and kill the page again, just so we can see what this gives us. So you can see that we've got this URI object. We will get all of this data when we cast this to a string. So let's just cast this to a string inside of here,
04:54
so it gives us back the full path. And you can see it includes page 2. That's not what we want, because the paginator needs to know the path here, so it can construct page equals
05:05
3, 4, when we output our pagination links. So already by just returning this, this will cast this to a string. So we don't need to do that manually.
05:15
But we do need to get rid of that end part. To do this, we can use the string to token method or function in PHP. And we can wrap all of this.
05:27
And then as the argument here, we're just going to give the query string question mark. And as you can see, if we just fix that by putting the argument in the right place,
05:37
you will be able to see that we just get the path to our app, which is great. That's all we need. So now we can take all of this and just go ahead
05:45
and return that directly from our path resolver. And we know that the path will always be correct now. OK, so the next thing is the query string resolver. This is pretty important, because it
05:55
needs to know what the current query string is if we need to append these on later. We'll be doing this later and see how this looks in action. But let's go ahead and add this in now,
06:04
because it's pretty straightforward. So let's go ahead and again pull out our closure. We're going to do exactly the same thing here. We're going to grab our container with our request.
06:14
And then we're just going to say get query params. We already saw that a little bit earlier when we set everything up. Now, the most important here is the current page resolver.
06:24
So current page resolver just needs to be what page we're currently on. Let's hard code it into two for now. And as you can imagine, when we refresh this,
06:33
we get the second page of results. Alex was on the first page, and Mabel was on the second, because we're splitting them by one. So we're going to do exactly the same thing as we did here.
06:42
So let's grab the query string. And then we can just read from that. So let's read the page name that we get passed into here. So let's say page name.
06:51
We'll set that as a default of page. And then here, we'll grab the page name. So basically, whatever's passed into the query string here under page will be read, sent back,
07:03
and then we can access that in there. And that gives us back the value of the page we're on. Now, as a default, we'll stick that to one, because we're always going to assume
07:11
that we'll be on the first page. OK, so now I'm going to go back to the home page. We're on the first page, so that's working. When we go to page two, that works.
07:21
And when we go to page three, rather than cause an error, it just gives us an empty set of results, which is absolutely fine. OK, so now that we've built our pagination,
07:30
we can use this anywhere. You can just iterate over everything as you normally would. Of course, the only problem is we don't have any links.
07:38
So let's go ahead and look at how we can create our own custom template, and more importantly, get the pagination package to actually use Twig to render this out.
50 episodes4 hrs 32 mins

Course overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

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

Comments

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