This episode is for members only

Sign up to access "Laravel Cursor Pagination" right now.

Get started
Already a member? Sign in to continue
Playing
03. Creating the posts API endpoint

Transcript

00:00
Okay, let's go ahead and build out the endpoint for our posts that we're going to see on that timeline.
00:05
First thing that we're going to do is go ahead and make a model, of course, so that's going to represent the post, and we'll go ahead and generate a migration alongside this as well. So let's go ahead and open up the create posts table migration, and let's come down and just fill in maybe just the body. Let's say table and text, and of course,
00:26
we're going to go ahead and call this body, and we can just go ahead and migrate that straight up. Now, we didn't generate a factory, so maybe we can go ahead and do that separately. So let's go ahead and say make factory, and we'll pass in the model in here to preset that, and let's go ahead and actually give this a name, post factory. There we go, and let's head over to
00:52
that post factory just so we can generate out some fake data, and of course, at the moment, we just have a body, so this is pretty straightforward. We'll go ahead and use faker to generate out a sentence here of maybe just 20 words. Great, so if we head over to our database really quickly and have a look in here, of course, it's empty. Let's go ahead and fill
01:13
that up using the factory that we've just created. So to do this, we can run php artisan tinker, we can go ahead and reference the post model, reference the factory method on this, choose how many times we want to do this, and let's just say 20, and let's go ahead and create this out. That's going to go ahead and create all of them records for us in the database. Great,
01:35
so we've got a database full of data that we can start to play with, and to play with this, we want to go ahead and create an endpoint to output this data. So we'll go ahead and create out a controller in here, and let's call that post index controller. That will deal with showing out posts, and we'll register this over in the API routes, because remember, technically,
01:59
we're hitting an API endpoint from a client, although it is within Laravel. So let's get rid of everything in here, and let's just go ahead and create this out, slash posts. We'll hook that up to the post index controller, like so, and we're pretty much done. So if we open up our post index controller, at the moment, there's nothing in here, of course. Let's just go and create an invoke
02:21
magic method in here just to deal with this, and we'll just go ahead and not use a API resource for this. We'll just say post latest and get, just to dump that data out. Of course, we could add an API resource later if we wanted to structure that data in a much better way. Okay, so now we can come over to our app, and we can go over to API slash posts and see all of our
02:45
data. Great. So we've got 20 in here, but of course, that's not what we want to do. We want to paginate this and later cursor paginate it. So let's paginate this by 10, and what Laravel will automatically do is output the data wrapped in this data property just here, but it will also give you all of the pagination information as well.
03:04
Now, this isn't the way we want to do it. When we switch this over to cursor pagination, we'll see something slightly different. So we'll take a look at that a little bit later. Okay, so now that we have our endpoint with all of our data in, we can go ahead and hook this up to the client and see the problem we're going to get when we try and load more posts.
6 episodes 25 mins

Overview

Let's learn about cursor pagination in Laravel. With a simple method change, you'll end up with better performance with larger data-sets and much easier implementation of infinite scrolling.

We'll build up a simple Vue app where we can post to a timeline, check out why standard pagination doesn't work and then switch to cursor pagination to fix it up!

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

Episode discussion

No comments, yet. Be the first!