Label synchronization
Objective
In the previous section, we implemented a selection of tags on the UI, and we could create multiple tags. But with flaws, today we're going to look at what the flaw is and fix it.
Description
Development environment: Windows 7
Laravel version: 5+
Ide:phpstorm
In the last lesson, we have implemented a label for the article, and can play multiple tags. At the end of the day, I felt that everything was still in the acceptance range, but I mentioned that there was a minor flaw, and I didn't know if the reader had found it. When you edit the article to change the label of the article, found that can be modified, but after saving to read the article, the label has not changed, or the previous label. What is this for?
This section of the content is relatively simple, specifically to solve this small flaw. Before you fix it, you'd better look at the flaw first.
Database check
Obviously modified, why did not change, I want to marry the database to see!
Sqlite> SELECT * fromarticles;1|1|2016-03-20 15:05:18|2016-03-20 15:05:18| Tan Xiaorong created articles | It's |2016-03-28 00:00:002|1|. 2016-04-23 07:00:13|2016-04-23 07:00:13|flashmessage|hello ~ flashmessage|2016-04-25 00:00:003|1|2016-04-23 07:07:34 |2016-04-23 07:07:34|flashtest|flashtest|2016-04-25 00:00:004|1|2016-04-23 07:37:10|2016-04-23 07:37:10| Session::put|session::p ut|2016-04-25 00:00:005|1|2016-04-23 07:59:52|2016-04-23 07:59:52| Tolstoy | Tolstoy |2016-04-25 00:00:006|1|2016-04-23 08:03:42|2016-04-23 08:03:42| on time plan to asda| the Aspen Frozen |2016-04-25 00:00:007|1|2016-04-23 08:05:52| 2016-04-23 08:05:52|asdasd|asdasdas|2016-04-25 00:00:008|1|2016-04-23 08:21:58|2016-04-23 08:21:58|autodisappear| autodisappear|2016-04-25 00:00:009|1|2016-04-23 08:39:20|2016-04-23 08:39:20|asd|asdasd|2016-04-25 00:00:0010|1| 2016-05-01 15:26:08|2016-05-08 14:17:44|asd|asdasdasdasd|2016-05-16 00:00:00sqlite> SELECT * Fromarticle_ tagwherearticle_id = 10;10|2|2016-05-01 15:26:08|2016-05-01 15:26:0810|3|2016-05-01 15:26:08|2016-05-01 15:26:08
You see, I just re-edited the article is the ID Number 10 of the article, the article called ASD, more unpretentious. Delete a label on the edit page, there are two, there is a supposedly said.
Then I went through select * FROM article tag where ArticleID = 10 to check the label of the article with ID 10, you see, or two, their tag numbers are 2 and 3 respectively. It's still two, not erased. Why is it?
Find out why
The method of updating the article is the Updat () method in articlecontrolle.php, right? Look at this method, compared to store () seems to be a little bit less, why compared with the store () method, because their process is similar. In the store () method There is a tags->attach () process more than the Updat () method. Let's try adding the statement to the update () method works:
Public Function Update ($id, Articlerequest $request) { $article =article::findorfail ($id); $article->update ($request->all ()); $article->tags ()->attach ($request->input (' tag_list ')); Return redirect (' articles ');
Then go to the article update page to modify the article's label, save and then view the article, found more tags ...
In fact, think about it, this $article tags attach is what? is the label of input (' tag_list '), which is equivalent to the new editor's label of the article should be stored in the database. If you don't believe it, you can check the tag of the article you just edited by sqlit3 order. Must have changed a lot.
According to this idea, we need to delete the tag stored in the original database, and then add a new tag, yes, that's the logic.
Laravel In addition to attach this method, there are attach anti-method detach, through the detach we can delete the specified label, you can do an experiment, but detach is not what we want to say.
Sync method
In the face of the above flaws, our solution is to delete the database of the article corresponding to the tag, and then add tag to the article. The equivalent, we first detach, and then attach.
However, Laravel also has a method called sync. The sensitive classmate has been able to fill its full name, yes synchronize, "synchronous" meaning. When you change the Attach method directly to sync, save and then edit the article, modify the label, save 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 ');
So, attach is adding tags, or binding tags, detach is to remove tags, or delete tags; sync is a sync tag, or a label update.
Similarly, replace the attach in the store () method with sync.
Summarize
A minor flaw was solved today, and three methods were compared, attach detach and sync.
Attach is to add tags to the article, binding tag, append tag
Detach is to give the article to unbind the specified tag, delete tag
Sync is used to synchronize the tag of the article.