Okay guys.
So next up we're going to cover the pagination.
So pagination is essentially revolving around the SQL queries.
And it means that we want the result of specific SQL query to be returned in chunks.
And those chunks are called pages.
You can customize the size of every single chunk.
So it can be ten elements per chunk or 20 or 50.
It's up to you.
So a quick reminder by using the pagination we will optimize the data fetching.
So it's always more efficient if you can fetch less items than if you have to fetch more.
And if you multiply every single request for a list of blog posts by, let's say, 300 users, we can
quickly see how getting just a chunk of the results instead of the full list of blog posts can be more
optimal for the compute power and for potential cost.
Now also, as we have previously said, recency matters and typically you will be fetching the items
that are more recent.
Okay.
So after this short reminder, let's just jump and see what we need to do to implement pagination.
We're going to start with the post controller as that's our entry point.
And I'd like to start with the parameters first.
So the page will be passed by the URL parameters.
And it will also have a default.
That's why I'm getting it from the Get super global.
So if page is not specified we assume the current page is one.
Now we can make the limit parameter.
So how many items per page should be displayed also part of the query parameters.
I'm just gonna leave it as static value, and I'm setting it to two just because we only have three
blog posts, if I remember correctly, loaded through fixtures.
So that's the only way we can see multiple pages.
Okay, so those were the parameters.
Maybe let's separate the parameters from database interactions.
So we would need to fetch the post.
But also we would have to know how many posts do we have in total when we apply all the criteria like
the search query.
So that's a separate thing.
We should add some kind of a method that can give us this info.
I might call it temporarily count and I need to pass the search query to it.
So it's non-existing right now.
So I comment that out.
Next up we should pass some data to the view like the current page.
That would be useful.
So we can generate links to different pages etc..
And also we need to know how many pages do we have.
And this can be easily calculated by dividing the number of total items.
So the total blog posts by the limit.
And rounding this up so we don't have the total value yet.
So I also comment this one out for now.
So let's jump to the database models shall we.
As we would have to make some changes maybe.
Let's start with this.
Get recent method as it also needs to incorporate the pagination.
So starting with parameters we've got the limit.
We've got the search query.
But we are missing the page.
Let me add the page parameter.
So it will also be nullable.
And if we scroll this method we already have the handling of a limit.
But this only limits the amount of results that we're going to receive.
To also add pagination we need offset.
So to understand the concept of the offset, let's take a look at this diagram.
Again assuming we would like to get ten items per page.
The offset for the first page would be zero.
And then we just grab the ten consecutive items.
So including the item at position zero, the last one would have the index nine.
For the second page, we need to increase the offset by ten so it becomes ten and we take ten items.
So the last item is 19.
Consequently, for the third page works the same.
We increase the offset by the amount of items per page.
So if it's ten the offset for the third page would be 20.
So we can leave this if statement checking for the limit, because sometimes the limit can serve different
purpose.
In this method we can just limit the recent posts to let's say five most recent posts.
So we should only add the offset if both the page is not null And when the limit is not null, this
means someone actually expects the pagination, and in such cases we should calculate the offset.
So the offset.
Well, we need to work with the page.
If page is one we need to subtract one from it and multiply by limit.
This is how we are going to receive what I've explained previously on the diagram.
So for the page that is one.
The offset starts at zero.
But for the second page the offset is page minus one times limit.
That's ten.
K.
And then we need to add something to the query.
So let's use concatenate equals.
And in SQL this is just called Offset.
Offset.
Keyword and the parameter.
So we need to add the calculated offset to the params.
And we've got the pagination.
If we now jump to the post controller where we actually want to paginate data, we can pass the page
as the second parameter or just use the named parameters.
Next up, let's solve the missing count of the total items.
This needs to take into the account the search query if there is any.
So let's add this method because it's missing.
And I'm gonna start by copy pasting the get reset method because the count method would be similar to
it, but just simpler.
So in the count method we don't need the limit nor page because this doesn't make sense in that context.
We want to count everything in this table.
We only need the potential search query.
It's the same as above.
We get the database, we start with the query, and we need to take into account the search query if
there is any.
But we don't need to do any sorting.
There is no limit.
No pagination can remove this fragment.
And the query is slightly different because this query here is fetching all the columns.
And to count the elements using a specific query, you would use count asterisk.
This would instead just give us one single value.
So also that's not fetch all.
this is a query method.
There is no class in here because we would like to get one single value.
So from the query method we can also get one single column value using fetch column.
By default this would return the value at column zero, and since this returns a single value which
would be a number, this is exactly what we need.
And even better, we can cast this to an integer to make sure it is an integer and just add return value
here, which is int.
Let's go back to the post controller now.
So we are missing the links to navigate between the pages, but we're going to do that in the next video.
Though it shouldn't stop us from testing this logic.
So this first parameter this actually needs to be the limit.
So let me change that to the limit variable.
Now we are using the variable in case later on we want to change this to a URL query parameter.
And after I save the changes and I go to posts, I should see only two blog posts instead of three.
And now let me just manually type the query parameter.
So we are on the first page.
That's the default one.
Now let me go to the second page and I think this makes sense.
That was my first blog post, which just confirms that everything works fine.
And all that's missing now are the buttons properly rendered below the blog posts that will take us
to the previous and next pages, and we're gonna do that in the next video.