[Laravel5Fundamentals] 23-SyncingTags tag synchronization
Preface
In the previous section, you can select tags on the UI and create multiple tags. But there are flaws. today we will look at what flaws are and solve them.
Description
Development Environment: Windows 7
Laravel version: 5 +
IDE: Phpstorm
In the previous lesson, we added labels for articles and added multiple labels. At the end of the day, I felt that everything was still in the acceptable scope, but I mentioned that there was a flaw, and I don't know whether the readers found it. When you edit an article and want to modify the label of the article, you can find it can be modified, but save it and read it again. the label remains unchanged. What is this?
The content in this section is relatively simple, specifically to solve this flaw. You 'd better take a look at this flaw before solving it.
Database check
Why is the modification not changed? I want to check it in the database!
Sqlite> select * fromarticles; 1 | 1 | 15:05:18 | 15:05:18 | the article created by Tan Xiaolong | true | 07:00:13 | 1 | 07:00:13 | flashmessage | hello ~ Flashmessage | 07:07:34 | 1 | 07:07:34 | 07:37:10 | flashtest | 07:37:10 | 1 | session :: put | 07:59:52 | 1 | 07:59:52 | 2016-04-23 08:03:42 | thostee | 08:03:42 | 1 | asda | scheduled on time | 1 | 08:05:52 | 08:05:52 | asdasd | asdasdas | 08:21:58 | 1 | 08:21:58 | 08:39:20 | autodisappear | 08:39:20 | 1 | asdasd | 15:26:08 | 1 | 14:17:44 | asd | asdasdasdasd |: 00: 00 sqlite> select * fromarticle_tagwherearticle_id = 10; 10 | 2 | 15:26:08 | 15:26:08 | 15:26:08 | 3 |
You see, the article I edited just now is an article with id 10, named asd. On the editing page, a tag is deleted. There are two tags, and there is only one tag.
Then I used select * from article tag where articleid = 10 to query the tag of the article with id 10. you can see that there are two more articles, their tag numbers are 2 and 3, respectively. Or two, not deleted. Why?
Find the reason
The update method is the updat () method in ArticleControlle. php, right? Let's take a look at this method. compared with store (), it seems that something is missing. why is it compared with the store () method, because their process is similar. The store () method has a tags-> attach () process more than the updat () method. We try to add this statement to the update () method:
public function update($id,ArticleRequest $request){ $article=Article::findOrFail($id); $article->update($request->all()); $article->tags()->attach($request->input('tag_list')); return redirect('articles'); }
Go to the article update page, modify the article tag, save the tag, and view the article again. it is found that there are too many tags...
Actually, let's take a closer look at what the $ article tags attach is? Is the tag in the input ('tag _ list'), which is equivalent to saving the newly edited tag of the document to the database. If you don't believe it, run the sqlit3 command to query the tag of the article you just edited. It must have increased.
According to this idea, we need to delete the tags stored in the original database and then add new tags. that's the logic.
In Laravel, apart from the attach method, there is also the attach's anti-detach method. through detach, we can delete the specified tag. you can do an experiment, but detach is not what we will talk about.
Sync method
In the face of the above flaws, our solution is to first delete the tag corresponding to the article in the database, and then add the tag to the article. This is equivalent to detach and attach.
However, Laravel has another method called sync. Sensitive students have been able to make up their full names. that's right, synchronize, "sync. When you change the attach method directly to sync, save it, edit the article, modify the tag, save it, and view the article, everything is back to normal:
public function update($id,ArticleRequest $request){ $article=Article::findOrFail($id); $article->update($request->all()); $article->tags()->sync($request->input('tag_list')); return redirect('articles'); }
Therefore, attach is to add or bind tags; detach is to remove or delete tags; sync is to synchronize tags, or update tags.
Similarly, change the attach in the store () method to sync.
Summary
Today we have solved a small flaw and compared three methods, attach detach and sync.
Attach is to add tags, bind tags, and append tags to an article.
Detach is used to unbind a specified tag from an article and delete the tag.
Sync is used to synchronize the tag of an article.