Planning Tags Feature 0:00Let's add one new feature. It would be nice if we could associate tags with any post. This is standard for any WordPress installation. So, how exactly would we do that? Well, we'll need to introduce a brand new relationship type. So first, let's think about structure. We're going to have a table of posts, but we should also set up a table for all of the various tags that can be applied. But next, we need some way to associate one post with one tag. So, for example, we should be able to say the post with an id of 1 should have a tag with an id of 1. Using a Pivot Table 0:21But next, we need some way to associate one post with one tag. So, for example, we should be able to say the post with an id of 1 should have a tag with an id of 1. But also, the post with an id of 1 should have a tag with an id of 5. That's the way we're going to do this. So we tackle that through a pivot table. And following a convention, we can name it post tag. That would be the alphabetical order of the two tables you're joining, and then the singular form, or the model name. So in this case, it's not tag post, it is post tag, because P comes before T. Or, of course, you can name it whatever you want and override the default. Creating Tag Migration 0:49So in this case, it's not tag post, it is post tag, because P comes before T. Or, of course, you can name it whatever you want and override the default. Okay, so it sounds like we need to create these two tables. phpArtisan. We're going to make a model called tag, and I do want a migration with that. Let's switch over to that, create tags table. Now first, a tag is pretty simple. It's just going to consist of a name. However, that should be unique. So we don't want multiple records in this table with the exact same tag name. Building Pivot Migration 1:16However, that should be unique. So we don't want multiple records in this table with the exact same tag name. But next, while we're here, let's also set up the pivot table. So I'm going to copy this and create another one, and then we'll drop it down here. Okay, so we'll set up our pivot table. A pivot table, whether to add the auto-incrementing ID, it just kind of depends, but we're not going to worry about it in this case. Next, we'll need an integer for the post ID, and then another one for the tag ID.Next, we'll need an integer for the post ID, and then another one for the tag ID. That's how we create the relationship. Next, no timestamps are necessary here, but we would like to set a primary key that says the combined IDs here have to be unique. So I don't want to end up in a situation where the post with an ID of 1 can be assigned a tag with an ID of 1, and then redo that row over and over. No, that combination has to be unique.and then redo that row over and over. No, that combination has to be unique. So we'll say set a primary key that consists of the post ID as well as the tag ID. Those two together create the primary key. Okay, so let's go ahead and migrate our database. Take a look in SQL Pro, and there we go. We now have tags as well as post tag. So yeah, let's set up a couple dummy ones here, like personal, and then maybe another one for PHP. All right, so now we use the pivot table to specifyand then maybe another one for PHP. All right, so now we use the pivot table to specify that the post with an ID of 1 should have a tag with an ID of 1. Or in other words, this post has been applied this tag. And then later, if we need to give it another tag, you could just create another one. Now the post with an ID of 1 has two separate tags. So how do we fetch these relationships? Well, here's what we can do. Let's go to our post model, scroll down, Defining Many-to-Many Relations 2:59Well, here's what we can do. Let's go to our post model, scroll down, and like I said, we're going to set up a brand new relationship. It's not a belongs to, like you've learned. It's not a has many. It is a belongs to many. It's a many-to-many relationship. And why is it that way? Because one post may have many tags, right? But that doesn't mean each of those tags belongs to the post.Because one post may have many tags, right? But that doesn't mean each of those tags belongs to the post. As it turns out, any tag may be applied to many posts. Notice that, many-to-many. Any tag may be applied to many posts. Or any post may be applied to many tags. It's a many-to-many relationship. So we use belongs to many, and then we give the name of the connecting table. So in this case, that would be tag.and then we give the name of the connecting table. So in this case, that would be tag. Okay, so take a look at this. If I open up PHP Artisan Tinker, and we find the post with an ID of one, I can now fetch all of the tags by referring to this relationship. Post tags. And whoops, relationship must return an object. Oh, sorry, forgot to return it. Okay, one more time.Oh, sorry, forgot to return it. Okay, one more time. So we fetch the tags, and there you go. Two tags have been applied. You could even say post tags, and then I want to pluck the name, and now we'll get an array of personal and PHP. But now, what about the inverse? For example, maybe you have a tag, and you want to figure out which posts are associated with that tag.For example, maybe you have a tag, and you want to figure out which posts are associated with that tag. Okay, well, it's the exact same syntax. So let's go to our tag, and add a new method here called, well, posts. A tag belongs to and has many posts. All right, we're done. So we run it again, and we'll say, app tag, give me the first one, and then let's find which posts have been assigned to this.app tag, give me the first one, and then let's find which posts have been assigned to this. Whoops, plural, should be singular. There we go. One post has received this tag. Now though, one other thing. If we fetch all of our posts, right now, I don't know which tag they have. And yes, you could fetch the relationship for each one. So you could do like a for each. For each tag says tag, then call this relationship. Eager Loading to Avoid N+1 5:05So you could do like a for each. For each tag says tag, then call this relationship. But one issue there is, you'll end up with what we call the n plus one problem. When you reference this relationship, it's going to perform an additional database query. So if you have 50 posts, and you filter through them, well, for each one of those, you're going to perform another database query. One more query.you're going to perform another database query. One more query. That's why we call it n plus one. So Laravel has the facilities to eager load this, which means it does it all in one query. So once again, take a look. Each post has no reference to the tag. But if I were to say, I want my posts with the associated tags, and then give me the results,I want my posts with the associated tags, and then give me the results, now you'll see, take a look. The first post has a collection of tags, and those have been eager loaded out of the box. But you'll see other ones are empty, which is good. That's exactly what we want. So finally, to finish up for this episode, before we manually added these, but you wouldn't be doing that in real life. Attaching and Detaching Tags 5:57before we manually added these, but you wouldn't be doing that in real life. Here's how to do it. We can say, let's find a post. All right, this post. And now I want to assign it a tag. So let's say app tag, where the name is equal to personal. Let's just find that one. Okay, so I want to associate this post with this tag.Let's just find that one. Okay, so I want to associate this post with this tag. We can say post tags, and we're not going to say save or create like we've learned. With a many to many relationship, we are going to attach a new record in the pivot table. And we'll give it either the model or the ID of the model. I usually just do the model. Okay, so let's switch back, give it a refresh. And there you go, it worked.Okay, so let's switch back, give it a refresh. And there you go, it worked. So just as we have the attach method, there's also a detach method that you could reference. Now if we come back, it's been removed. Very cool. So in the next episode, let's figure out a proper way to display these tags for each post. And then even filter all posts according to their tag.And then even filter all posts according to their tag.