Routing Tagged Posts 0:00Building on the last episode, I've set up three records in our pivot table. So these three different posts have these three tag IDs. Let's now figure out how to display those on the page. So I'm going to go to my route slash web file, and maybe we could say, when you visit posts and then tags, followed by some kind of wildcard, then, well, we have a couple choices. We could do post controller index by tags, just set up a custom method there. Another option would be to call the index method, just like we do up here. But this time, what we could do is accept a tag here, a tag name, and then make it nullable. And whoops, we should import that.But this time, what we could do is accept a tag here, a tag name, and then make it nullable. And whoops, we should import that. So that way, it's still going to work. However, if we try to visit a specific tag, something like this, yeah, you'll see that we still hit this method. But this time, you'll see that it accepts the tag. And then further, you could get the posts associated with the tag, like we learned in the last episode. So yeah, at this point, you could just do something like, well, if we do have a tag here, then we want to perform such and such query. Creating Tags Controller 1:01So yeah, at this point, you could just do something like, well, if we do have a tag here, then we want to perform such and such query. Otherwise, we'll do the default. So that's an option as well. And then finally, a third option is to create a dedicated tags controller. And I think this might be a good way to go because you'll be doing more with tags. You'll be creating and storing tags. You might have an endpoint to delete an existing tag. So it can make sense to give it its own controller. So with that in mind, let's create it.So it can make sense to give it its own controller. So with that in mind, let's create it. Make a controller called tags controller. And now we're going to switch over there and receive an index method. So we know at this point that we're going to accept a tag, right? And let me import that. However, if we return this real quick and then switch over and give it a refresh. Yeah, we get the tag. But notice that we're using an ID here. That's not overly user friendly. Binding Tags by Name 1:50But notice that we're using an ID here. That's not overly user friendly. They probably just want to type in the tag name and then see everything, right? So how do we deal with route model binding when we're not using the primary key as the identifier? Well, here's what we can do. I'm going to visit my tag model. And now I'm going to add this new get route key name. So by default, it's going to be the ID. But instead, we want it to essentially perform a where condition on the name. So that way, if you type personal here, it's going to say,But instead, we want it to essentially perform a where condition on the name. So that way, if you type personal here, it's going to say, give me the tag where the name column is equal to personal and then get the first result. So now if I come back and refresh, we get the same thing. And that's a lot more user friendly. Okay, so at this point, we could just say post equals tag posts, and then return the same view as before. Like so. All right, let's give it a refresh. And there we go.All right, let's give it a refresh. And there we go. This is the only one for PHP. And then we had, what was the other? Personal. There we go. It looks like we have two that were assigned that tag. And if we switch over, yep, there we go. Let's make it three. Come back, refresh. Displaying Tags in Sidebar 2:58Let's make it three. Come back, refresh. And now we have exactly three posts that have been assigned that tag. Next, why don't we display all of our tags in the sidebar? So we're going to close all of these out. And I'm going to go into my app providers, app service provider. And here we can see that when we load the sidebar, we want to give it the archives. I'm also going to give it something else. We want to give it all of the tags. And this is actually a good candidate for caching.We want to give it all of the tags. And this is actually a good candidate for caching. Rather than performing a database query every time, fetch it once and then cache it indefinitely until you change it. But we haven't covered any of that yet. So anyways, we're going to say app tag and give me all of the tags. Or we could even say app tag and then just give me the name associated with it. So that will give us this end result. App tag, and then just pluck out the name column, because that's all we really care about.App tag, and then just pluck out the name column, because that's all we really care about. So now our sidebar will have tags. Let's switch over there. And why don't we put it right below? So we'll say we have our archives, and then we're going to have the tags. And we'll say for each tags as tag. Then we will link to posts, tags, and then the name of the tag. And we'll spit that out. All right.And we'll spit that out. All right. So let's see how that looks in the browser. We're going to give it a refresh. Whoops. Providers app tag not found. Yeah, I must not have imported it right. Yep, I forgot. So let's try it again. All right.So let's try it again. All right. Scroll down, and we have personal tags. Great. And then PHP tags. Ah, but in this case, you'll remember that we removed that record from the pivot table. So that's something we should think about. There will be situations when you have tags, but no posts associated with them. And if that's the case, it seems silly to display a PHP tag here if there's no post to view. So here's how we fix that. Filtering Unused Tags 4:42And if that's the case, it seems silly to display a PHP tag here if there's no post to view. So here's how we fix that. Let's open up PHP Artisan Tinker. We could say, give me all of the tags, but just as long as they have posts associated with them. And we're going to pluck the name. So this time, we'll get exactly one. But if we were to switch back and add another one. Okay, well, now we should get exactly two. And we do. So let's modify our query.And we do. So let's modify our query. App tag, give me the tags, just as long as each one has related posts, and then pluck the name. So that should do it. Let's go back home. And we should now see the two. But if we bring this back and give it a refresh, we should just see the one. Perfect. Now, let's do a little bit of cleanup. We could also pass an array to the viewWith method. Showing Tags on Posts 5:27Now, let's do a little bit of cleanup. We could also pass an array to the viewWith method. So I could say, viewWith, and then provide an array. Or also just do compact archives and tags. That way, I could grab all of this and then just store these as variables. And then do the exact same thing here. And that will give us the same thing. Finally, the only remaining step is, let's display the tag for each post. So I want to see that somewhere here. Okay, so we're going to go to post slash show.So I want to see that somewhere here. Okay, so we're going to go to post slash show. And then, yeah, find some good place to put it. I'll just put it right here. And we'll say, if we have any tags for the post, then we'll display them. And we'll just do something like this. For each post tags as tag, then spit out the tag name. All right, let's come back and give that a refresh. And there we go. And of course, that can then be an anchor tag.And there we go. And of course, that can then be an anchor tag. Post tags, and then the tag name there. All right, makes sense. Refresh. This has exactly one tag that you can style nicely however you want. But if we click on it, now we can see all posts that have received that tag.