Installing Laravel Breeze0:00
Let's start the backend of our blog application. I have a brand new Laravel app here. It's making use of Laravel 9, which just came out, which is great. Let's start off by installing Breeze, since we will do some authentication later on, and we want to make sure that we do this first. So let's go ahead and do that quickly. Let's composer require it. And let's install the API version.
Generating Post Model0:20
Let's composer require it. And let's install the API version. So that should be this one here. So we have to php artisan breeze:install API. And let's go ahead and migrate. Okay. Now let's go ahead and set up our Posts and migrations and all of that. So php artisan make:model Post -a for everything else.
Defining Migration Schema0:44
So php, artisan, make:model, Post, and let's say $a for everything else. Okay. Let's go ahead and start with the migration. So create posts table. Let's start with the foreign ID for the User. So table foreignId is gonna be a user, and let's say constrained, okay? And let's just have a string and a body here. So a string for the table, I mean, for the title,
Adding Model Relationships1:11
And let's just have a string and a body here. So a string for the table, I mean, for the title, and text is fine for the body. Okay. So that should be fine. Let's go ahead and set up our relationships. So User has many Posts. So let's add that down here. Post return this as many Posts, okay? And do it the other way as well for Post.
Creating Factories and Seeders1:36
return this as many Posts, okay? And do it the other way as well for Post. I'm gonna set guarded to an empty array as well. And let's say User return this belongsTo User. Okay. Save that. Let's go ahead and set up our PostFactory. So we have some dummy data. So let's start with the userId here. And for each Post, we'll just create a new User.
So let's start with the user ID here. And for each post, we'll just create a new User. So we'll say userFactory. So make sure to import User here. Make sure to add a title. And we'll make use of Faker for this. It's uppercase, the first word. And let's say this Faker words, say five words and make sure they are, or make sure it's a string here for the body.
say five words and make sure they are, or make sure it's a string here for the body. Let's say this Faker, let's use sentences. And let's say 12 words. And again, make sure it's a string. And for createdAt, say this Faker, say dateTimeBetween, and let's give it a random date between now and 30 days ago. So minus 30 days and now.
between now and 30 days ago. So minus 30 days and now. Okay. That should be fine. Let's go into our database seeder. And let's get rid of this here. And we'll just say PostFactory. Let's create 20 Posts and create. Okay. And I'm also gonna change the first User here,
Okay. And I'm also gonna change the first User here, just so we can test it out when we're doing authentication. So User::find(1), go ahead and update. And it's just updated to me. So let's say $name = 'Andre' and $email = 'random@example.com' here that I can make use of when I'm testing. Okay.
that I can make use of when I'm testing. Okay. Make sure to import User. Hopefully I did everything correctly. Let's migrate fresh --seed. And that looks good. Let's check out our database here. Refresh this. And we should have 20 Posts and a bunch of Users as well. Okay.
Building Posts API Endpoints4:11
And we should have 20 Posts and a bunch of Users as well. Okay. Now let's set up a few API endpoints. Maybe we'll create one for showing all Posts, one for showing a Post and maybe one for creating a Post, at least for now. So let's go to routes/api.php and let's set that up. So I'll leave this one in here for now. Let's set up the first one to grab all Posts. So let's name it /posts.
Let's set up the first one to grab all posts. So let's name it /posts. There should be a PostController in here. Should be the index method. And we don't really need a name since we're gonna be consuming it on the front end. But I'm gonna add one anyways. Let's do two more here. So let's add one for grabbing a single post. So /posts/{post}.
So let's add one for grabbing a single post. So posts/post. And that's going to the show method. And we'll name that postShow. And one more for creating a post. So that's gonna be a POST request, say to /post or /posts, whatever you want. I'll say post. And that goes to the store method.
I'll say post. And that goes to the store method. And that's gonna be post.store. Okay. Let's start with the posts endpoint that shows all posts. So let's go into our PostController, index. And we can return post::all or I actually want the latest first. So let's say post::latest() get(). Okay.
So let's say Post::latest(). Okay. This should be fine. Let's test it out in the browser or actually I'll just use my REST client here. So let's make a new endpoint here. Let's say /posts. It's a GET request. The endpoint is, what's my project name?
The endpoint is, what's my project name? lcnux3-blog-laravel. lcnux3-blog-laravel.test slash API slash posts, I believe. And we are getting an error. Maybe I forgot to import the controller in our routes here. Make sure to import PostController. Okay. And let's try that again.
Okay. And let's try that again. Okay. So that worked. And it looks like we are getting the latest on top. So that's great. Let's work on showing a single Post. So back to our PostController. Let's go to the show method. And for now we just have to return the post command.
Let's go to the show method. And for now we just have to return the post command. Later on, we might want to add the user, but this is okay for now. Let's make sure this works. So post/{id} here. Okay, that works as well. And let's also test create here or let's work on adding it first. So that should be store.
or let's work on adding it first. So that should be store. And you can see Laravel generated a store post request here. And you can use that if you like, but I'm just going to stick to the regular request here. So let's make sure to import Request here. And let's do some validation. So request validate. And let's just make sure the title exists. So title is required.
And let's just make sure the title exists. So title is required. And let's just say at least three characters and the same for the body. And that should be fine for now. And here we can create the post. So let's return post::create. And for now we're just going to hard code a user_id in here. But eventually this has to be the logged in user. So for now let's just say 1.
But eventually this has to be the logged in User. So for now let's just say one. The title is coming in from the request and same for the body. Okay, go ahead and test this out. So it should be a POST request. Believe I said /post. Go ahead and add a body here. This is fine. Let's just see if we get an error here.
This is fine. Let's just see if we get an error here if we send in the request. And we are getting this method not allowed error. Maybe if we set a header here to accept JSON, that should give us the proper errors. So let's accept application/json here. And now we get the correct errors. So now we can enter that information in our form. So title, this is the title.
So now we can enter that information in our form. So title, this is the title. Okay, so that works. body, this is the body. Okay, so that did add that new Post. Let's double check our database. And there it is, cool. So that should be a good starting point for our blog application. We'll start to wire it up to our front end in the next video.
for our blog application. We'll start to wire it up to our front end in the next video. Let's go ahead and make a commit for this repo. It's a separate repo from the front end. Believe we have to initialize it first. git add, git commit, blog starting point.
