تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

مرور نمایش یک رشته (Thread)0:00

Let's display a Post. Now, displaying a Post is going to be one of the most straightforward things that we can do because here we are going to rely upon Eloquent to automatically eager load the relationships so that we don't have to build, you know, a custom query to do all of that. There's really no need except to include the counts, which we'll talk about here. But I wanna start with the route

Defining Post Show Route0:28

which we'll talk about here. But I wanna start with the route because this is going to, uh, essentially define how we build our controller, or at least the signature of our controller. So I want it to look like this because if you look at other social media platforms, they follow a similar URL. So that's, you have the profile handle followed by a slash, and then status slash

So that's, you have the profile handle followed by a slash, and then status slash and then the post id. But in this particular case, I don't want that to be on the ProfileController because this is for displaying a post. So instead we're gonna have a PostController and we will have a method called show, and it's going to be named posts.show. So that's gonna be our route.

Enabling Scoped Bindings1:08

and it's going to be named posts.show. So that's gonna be our route. However, I want to take this a step further and we already have a relationship set up between the Profile and the Post. In fact, it's a belongsTo the Post, belongsTo the Profile. So we could rely upon Laravel to automatically check to be sure that the Post belongs to the Profile by calling scopeBindings. So this will make it so that we don't have to actually check.

by calling Scope bindings. So this will make it so that we don't have to actually check that inside of our controller. Laravel is going to do it automatically for us, and I am all for that. So this just saves us a little bit of time. It also makes our controller just a little bit cleaner. Uh, I know we don't have this controller class yet. Let's go ahead and let's include that here in our use statements.

Implementing Show Controller1:54

Let's go ahead and let's include that here in our use statements and then we will create that controller. Uh, there is an error and maybe it's because yeah, it can't find it, so we're gonna be okay there. So now let's make our controller PostController and let's open it up because we want to implement that show method. So public function show, we want the profile.

because we want to implement that show method. So public function show, we want the profile and we want the post. And once again, we don't have to do any checking because it's automatically going to check for us. Now, when it comes to our post, we need to be sure not to choose the component. We want our model, and that's that. So we are going to return a view that will be posts.show,

So we are going to return a view that will be posts.show, and we simply want to provide it the profile and the post. Now, I don't necessarily know if we need the profile. In fact, you know, thinking about it, all we are doing is displaying the post, and the post is gonna have the profile. So really we just need to do that right there. Send the post and we're gonna be fine,

Eager Loading Counts Replies3:00

So really we just need to do that right there. Send the Post and we're gonna be fine, but we need to load some things for the Post, such as the counts for the replies and things like that. So we are going to load the replies so that we can get its counts, but we also want to eager load, you know, some of the information about the reply. So we're gonna start though with count, and we want the likes, the replies,

So we're gonna start though with count, and we want the likes, the replies, and the reposts, but we also wanted with the profile and the parent profile, and let's order these ascending just like we did in the replies page on the profile. And that's gonna be it. Except that we also want to load the count

And that's gonna be it. Except that we also want to load the count for the Post itself that we are going to display. So once again, we want the likes, we want the replies, and we want the reposts. And I keep thinking we need to write something to make that easier to do, but I, I never get to it. And for the most part, that's it. It's very simple, it's very straightforward. We already have the Post, we just need

Creating Post Show View4:07

It's very simple, it's very straightforward. We already have the Post, we just need to load the extra information. And so now we just need to create this show view. So let's open up explore here and let's go to posts inside of our views. And we're gonna create a new file called show.blade.php. And I think, you know, we could use the feed view that we extracted many episodes ago. I mean there, there's still some things that we need to do.

that we extracted many episodes ago. I mean there, there's still some things that we need to do because this has things that we just don't need. Like for example, there's the post prompt that we don't need here because all we are doing is displaying a Post. But then too, we don't have feed items, we just have a Post that we want to display. So really what we want to do is use our component and the Post is going to be the Post that we provided.

So really what we want to do is use our component and the Post is going to be the Post that we provided. And then we also want to set showReplies to true because the default is false. You know what, we're gonna take a tangent because if we look at profiles and then show, let's look at the profiles page. You know, there's one of these that has replies, this one right here. And if you'll remember, the replies were being displayed.

replies, this one right here. And if you'll remember, the replies were being displayed because they were eagerly loaded, Eloquent saw that we were trying to display the replies. So it fetched them for us and saved us from having to do that ourselves. But all I needed to do is this. So this is the show view for the profile show replies is set to true. No, we don't want to show replies set to false.

for the profile show replies is set to true. No, we don't want to show replies set to false. In fact, we could just omit that completely because the default is false, which means that inside of the post component we don't have to check, you know, if the replies were loaded because we don't. And I'm pretty sure that you were screaming at me in the previous episodes saying you have show replies just pass false. Yeah, sorry, couldn't hear you.

just past false. Yeah, sorry, couldn't hear you. Okay, but sanity check, let's make sure that that works. Sure enough, okay, sidetrack, done. We are okay to continue. So we are inside of the show view for the posts. We now are displaying a post, we are showing the replies, and I don't think that there's anything else that we need to do here.

and I don't think that there's anything else that we need to do here. But one thing we need to think about is that a reply could also have replies. And at some point in time we need to set some kind of limit because it, it seems kind of ridiculous to have a reply that has a reply, that has a reply, that has a reply and, and and so on and so forth. But we're not gonna worry about that right now. But what I do want to do is essentially say that, hey,

But we're not gonna worry about that right now. But what I do want to do is essentially say that, hey, inside of our Reply component, we can also show replies. So let's take this whole block of code here and it needs to go before the closing div tag. So instead of our Reply component, there's our closing div tag. We're gonna put the reply stuff there and great, except that's really what we want to do is pass down, you know, those properties.

and great, except that's really what we want to do is pass down, you know, those properties. So whatever was passed to this Reply component, we need to pass it down so that if showEngagement was set to false, then that would be passed on down. The same would be true for showReplies. So we'll have that and we should probably do that inside of the Post component as well. So let's just copy that and we will paste it here.

of the post component as well. So let's just copy that and we will paste it here. And yeah, that should take care of that so that now if a reply has a reply, it should be loaded and we should be good to go. So now all we need to do is go to a page that is going to display a Post. And I just happen to know the ID of one of those. It's the ID of 67. And here we can see we have our post, the main post,

It's the ID of 67. And here we can see we have our Post, the main Post, then we have a reply, but then this reply has a reply. Now, full disclosure, behind the scenes, before I even turn on the camera, I changed the database so that there would be a reply to this reply. But I'm also noticing that we're not loading the counts for that, which means we will need to do that. But at least we can see that we have our Post, we have a reply,

But at least we can see that we have our post, we have a reply, and if there are any replies to a reply, then we see those as well. But you know, we're gonna have to change what we load here. Um, you know, because as it is, we have to manually do this. I mean, we could write something that would recursively load everything that's getting into the weeds a little bit. I mean, it's something that we would have to do,

load everything that's getting into the weeds a little bit. I mean, it's something that we would have to do, but I, I don't know if that's something that we need to spend time on right now. So I'm gonna do this, we we're gonna have two levels of replies. We have our main replies here, but then we are going to load with the profile, with the parent profile and then with the replies, which is going

with the parent profile and then with the replies, which is going to be the same dadgum thing. So we have two levels of replies and I think that should work. So if we go back, now we have the counts for the reply to the reply, but now we just need a link so that we can go to these individual posts. And I think using the dateTime that it was created, I think that would be a good enough thing.

Linking Posts by Timestamp9:44

And I think using the created_at that it was created, I think that would be a good enough thing. So let's go to our Post component. Let's find the created_at. There it is right there. So we are going to turn this into a link. And now that we have a route for this, we can use that route. So that, let's see, what did we call it? It was posts.show. Then we wanted the post profile and the post.

It was posts show. Then we wanted the post profile and the post. So let's copy that a element so that we can put that inside of the reply view. We just need to find created_at, I, I swear there's got to be some way that we can combine the replies and the posts, and maybe I just need to spend some time to do that. But for right now, that should be okay. Internal server error, missing required parameter.

But for right now, that should be okay. Internal server error, missing required parameter for route post show profile, missing parameter post, are we? No, we're not. But of course it helps if you do this correctly. We need to pass this as an array. So we have post profile and post, then over here, let's put this on to different lines to make this easier.

and Post, then over here, let's put this on to different lines to make this easier. And once again, we need this as an array and that should fix that so that now sure enough, we have links so that we can go to these individual posts. If we click on any one of these, then we see that post and everything is as it should. So now we are pretty much done with everything that, uh, an unauthenticated user can do. The only other thing is seeing what artists to follow,

an unauthenticated user can do. The only other thing is seeing what artists to follow, but we can use that as a transition point so that unauthenticated users just see a list of profiles to follow, and then authenticated users see a completely different list based upon who they already follow. So we'll tackle that in the next episode.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟