Define Post Store Route0:00
Let's create a Post. Let's start by defining our route. And since we already have a PostController, it just makes sense to, you know, follow the standard operating procedure so that we'll have /posts as our URL and that will map to our PostController that will have the store method so that then the name of this will be Simply PostsController store. So now instead of our PostController, we just need
Implement Store Method0:31
of this will be Simply Posts store. So now instead of our PostController, we just need to define that store method. But you know, typically we have a special request class to handle authenticate non authentication of validation there. So we could have something like CreatePostRequest. So that's, we could have that request. And then, you know, what do we do? We get the profile, which will be from the users so
And then, you know, what do we do? We get the profile, which will be from the users so that we can get the profile there. And then we will get the post. And we have that published method that we created so that all we have to do is provide the profile and then the content from the request and that's gonna create that post. And then I guess we could just return redirect to the posts index.
Create Post Request Validation1:15
And then I guess we could just return redirect to the posts index. That way we can see the new timeline that has our new post. And yeah, that should be it. So we need to create that request class. So we will use artisan to make the request createPostRequest. Let's open that up. And you know, our route is already protected, but let's also just use auth check inside.
And you know, our route is already protected, but let's also just use auth check inside of the authorized method to ensure that this request can only be used by an authenticated user. And then our validation rules, we, we have one thing, just the content really, and that's it. And you know, we could make the argument that, you know, a repost is a post, so we could use this same request class. I don't know if we want to or not. When it comes time to implement that. We'll, we'll see.
I don't know if we want to or not. When it comes time to implement that. We'll, we'll see. But content is required. Do we really need that? It's a string, but I'm gonna say string and then we haven't really talked about, you know, a max number of characters, but I think a max of 280 is gonna be fine. Then we will have our messages method that will return any error messages. And really, there's just one that I can think of.
that will return any error messages. And really, there's just one that I can think of because everything else would be, you know, understandable. And that is content_max. And we can say post_content cannot exceed, uh, 280 characters. So there we go. We have our validation all set up to where we, well we need to add a use statement here and then we need to show this. So inside of the index view, uh, yeah, that's
Build Post Form Component2:44
and then we need to show this. So inside of the index view, uh, yeah, that's where we have our partial, now we've been getting away from partials and using components. So I, I'm thinking we do the same thing here so that we just create a component, it, it'll serve the same purpose. I don't see a need to separate the main, you know, creating a Post and then replying.
I don't see a need to separate the main, you know, creating a post and then replying. Is that what we did? Oh, well I think it's the reply. Uh, I don't see any need to really change how this works. It's just, you know, using a Post form component just starts to fall in line with everything else so that then, you know, we could have the labeled texts, which would be post body, then we would have the field name, which in this case, uh, I know it says post here,
then we would have the field name, which in this case, uh, I know it says post here, but really we need content because that's what, you know, our request class is expecting. And then finally we would have the placeholder. So we would have that and in this case it would need to say what's up and then the profile handle. But we don't have the profile here, do we?
and then the profile handle. But we don't have the profile here, do we? We just pass in the posts. So we need to supply the profile to the index view. So that's not only do we use that here so that we say profile handle. And then we have the question mark at the end. But we also need to, you know, make some changes here. Instead of /profile. We want to output the route for profiles.show.
Instead of /profile, we want to output the route for profiles.show. I think that's what it was. And we'll pass in the profile then for the avatar, we have the profile avatar, url. Then when it comes to the name here, this is the displayName. I think that's the displayName. Yeah, that's the displayName. But we also need to do this, we need
Yeah, that's the display name. But we also need to do this, we need to provide the action URL here, which is gonna be route and then post.store, because I don't think there's an action. If we open up the, what is it? post_form.blade.php? Yeah, there's action, but we weren't setting anything there. We also need to change the method here, but let's create this component first so
We also need to change the method here, but let's create this component first so that we will make the component PostForm. We only need the view. We don't need a class, I think. So let's open up PostForm component. We'll paste in that markup. Everything else is gonna be the same, except that now we have an action that we can use here. Let's set the method to post, because I think regardless if we are creating a post
Let's set the method to post, because I think regardless if we are creating a Post or uh, creating a reply, it's still gonna be a post request label, text area. Everything else should be fine. And I don't think we did anything custom later. Nope, I don't think so. So I think we are, okay, let's get rid of this. Include, and we should be able to just load this up in the browser and submit our form.
Test Form Submission5:39
Include, and we should be able to just load this up in the browser and submit our form. Right? Let's try it. And we submit our posts and page expired. You know why we got the page expired because we need the CSRF. Let's go back and let's do a hard refresh. So let's try it again and we will post. It should redirect us back. And sure enough, there is our new post.
Wrap Up and Next Steps6:09
It should redirect us back. And sure enough, there is our new Post and nothing spectacular here. I mean, this is normal everyday stuff. create, read, update, and delete so that now we can focus on making a reply, which we will do in the next episode.
