Pivot table not updating 0:00If you watched the very last lesson, I sort of ended with a cliffhanger. For example, we have two tags here. But let's imagine that the coding part was a mistake. This really is just personal. All right, so I'm going to edit the article, remove it, and if I update it, well, crap, it still exists right here. And we can prove that. I have a SQLite prompt here. Select star from articles. And that article has an ID of 16.Select star from articles. And that article has an ID of 16. So select star from article tag, where article ID equals 16. And yeah, we still have two tags in the database. So, hmm, what's the problem here? And if you think about it, it's pretty obvious. I'm going to switch back to my articles controller, where we update an existing article right here. Well, notice in this case that we updated the article model in the articles table, but we paid no attention to the actual pivot table. Naive attach attempt 0:51Well, notice in this case that we updated the article model in the articles table, but we paid no attention to the actual pivot table. And actually, that's only step one in fixing this issue. But let's tackle that first. So let's see. If I go to my store method, here's the section we need. Remember, we take the article, the existing article, we find its related tags, and then we attach a new one in that pivot table. So what if I grabbed this and I just copied and pasted it down here? Well, would that work?So what if I grabbed this and I just copied and pasted it down here? Well, would that work? Take a moment and think about it if you need to. And well, let's try it out in the browser. I'm going to edit this once again. We remove coding. I update the article. And let's see. Crap, now it's even worse. So before we had personal encoding, but now we actually have another reference to it.Crap, now it's even worse. So before we had personal encoding, but now we actually have another reference to it. Hmm, let's try this out. Let's run our query once again. And yeah, notice that we added this twice. So very clearly, this is not what we want. Now, we actually have a couple ways to solve this. One would be to, on the MySQL end, ensure that a pair like this is always unique. And that way, it physically won't allow you to create a new record that has an article ID of 16 and a tag ID of 1. Using sync for tags 2:10And that way, it physically won't allow you to create a new record that has an article ID of 16 and a tag ID of 1. That would be one option. But in our case, I want to solve it in a different way. So how could we essentially sync everything up so that I can say, whenever I run this method, just make sure that this array of tag IDs I give you are the only ones that exist on the pivot table for that model? I hope that makes sense. So as it turns out, that term sync is actually the keyword here.I hope that makes sense. So as it turns out, that term sync is actually the keyword here. So this time, I'm only going to make one change here. Rather than an attach method, and by the way, there's also a detach method that would do the opposite of that. But then we also have one called sync. So this still accepts an array of IDs. However, the key thing to understand here is this array of tag IDs will be the only ones in the database associated with this specific article ID. So that means Laravel will take care of the deleting and the adding automatically for you.will be the only ones in the database associated with this specific article ID. So that means Laravel will take care of the deleting and the adding automatically for you. How cool is that? Why don't we try it out? But really quick, first, let's do a bit of cleanup. Let's fix our mistake from earlier. So, well, you know what? Why don't we just delete from article tag? Empty out that table. All right, back to Chrome.Empty out that table. All right, back to Chrome. We are on the edit page. I'm going to add personal and coding like we did before. Update it, review it, and there we go. But now we're going to make a new change. And this time, like we did at the beginning of the video, I'm going to remove coding. And if I update the article this time and take a look, it works. So let's review this.And if I update the article this time and take a look, it works. So let's review this. Run it again. And this time, as we needed, that second record in this table that associated the article with the coding tag has now been removed. And that's specifically because we used the sync method here. So just remember, attach when we want to add new rows into the pivot table. Detach when you want to remove them. And then sync when you want to sync everything up. We provide an array of, in this case, tag IDs. Refactor syncTags helper 4:17And then sync when you want to sync everything up. We provide an array of, in this case, tag IDs. And only those IDs will be associated with the article in the pivot table. Anything else on the table will be deleted. So why don't we do this? Why don't we extract this to a private method on our controller called sync tags. And if we run this, we're probably going to tweak it a little bit. I don't want to pass through the full article request. So why don't we change this up and instead pass through the article and then the tag list. Like that.So why don't we change this up and instead pass through the article and then the tag list. Like that. Okay. So the article and then the array of tags. And then we can update that here. Sync up the list of tags in the database. Now we can use this same method back within our store method where we create an article. So right here, this sync tags, we pass through the article. And once again, the tag list. Finally, why don't we extract this to a method as well?And once again, the tag list. Finally, why don't we extract this to a method as well? Because what are we really doing here? Well, we are creating a new article or publishing an article or however you want to represent that. Let's keep it simple and say this create article and then pass through the request. Okay. I will add that method and paste this in. That way I can return article at the bottom. And then let's type in this article request and create my doc box. Save a new article. Review controller structure 5:40And then let's type in this article request and create my doc box. Save a new article. And then finally up here, create a new articles controller instance. Cool. All right. So let's review this controller. Still very clean. We have our index method where we fetch the latest published articles and we load a view. Great. We have a method to show an article where we simply use route model binding to get the article.Great. We have a method to show an article where we simply use route model binding to get the article. And then we load a view and pass it through. Next, we have a page to create an article. We fetch some tags for the select dropdown and we load a view. Next, the actual process of persisting or storing a new article. Well, we do a bit of automatic validation against the form. And then if all of that passes, we create the article. We flash a message and we redirect. Notice how easy this is to consume.We flash a message and we redirect. Notice how easy this is to consume. There's no lines here where you have to take five seconds and figure out now what exactly is happening here. Whenever you find yourself doing that, extract it to a readable method. Now, all I need to know is we create an article. And then later, if you actually need to review that process, you can dig a level deeper. But on the surface, this is all we need. We're creating an article. Next, we have a page to edit the article.We're creating an article. Next, we have a page to edit the article. So we fetch tags again and we load the view. Quick note. Notice how we duplicated that there. I brought this up in the last video. Be thinking about this. Is there a way to automatically bind a list of tags for that form? We'll get to that later. Next, we update an article.We'll get to that later. Next, we update an article. Once again, we perform some validation using that same form request class. And then we update the article. We sync the tags. And we redirect back to the article list. And then finally, since our project is still pretty small, it's okay to have this stuff in here. You are not going to burn if you have a private method in your controller. So we have one for syncing our tags.You are not going to burn if you have a private method in your controller. So we have one for syncing our tags. And then another one that orchestrates the process of how we create and persist a new article. All right. So that'll do it for this lesson. You learned how to sync up your pivot tables and also how to do a bit of refactoring.