Table of Contents
标签同步
数据库检查
找原因
sync 方法
总结
Home Backend Development PHP Tutorial [Laravel 5 Fundamentals] 23 – Syncing Tags

[Laravel 5 Fundamentals] 23 – Syncing Tags

Jun 20, 2016 pm 12:29 PM

标签同步

前言

上一节,我们在 UI 上实现对 tag 的选择,并且可以创建多个 tag 。但是有瑕疵,今天我们就来看看瑕疵是什么,并解决它。

说明

开发环境:Windows 7

Laravel 版本: 5+

IDE: Phpstorm

上节课,我们实现了给文章打标签,并且可以打多个标签。结束的时候,感觉一切都还在接受范围,但是我提到有一个小瑕疵,不知读者有无发现。当你编辑文章想修改文章的标签的时候,发现可以修改,但是保存之后再看文章,标签没变,还是之前的标签。这是因为什么?

本节的内容比较简单,专门解决这个小瑕疵。解决之前,你最好先看下这个瑕疵。

数据库检查

明明修改了,为什么没变,我要娶数据库里查看一番!

sqlite> select * fromarticles;1|1|2016-03-20 15:05:18|2016-03-20 15:05:18|谭晓龙创建的文章|真的是|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::put|2016-04-25 00:00:005|1|2016-04-23 07:59:52|2016-04-23 07:59:52|托尔斯泰|托尔斯泰|2016-04-25 00:00:006|1|2016-04-23 08:03:42|2016-04-23 08:03:42|按时打算asda|阿斯达速冻|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 
Copy after login

你看哈,我刚才重新编辑的文章是 id 号为 10 的文章,文章名为 asd ,比较朴实无华。在编辑页删了一个标签,原来有两个,还剩一个按理说。

然后我又通过 select * from article tag where articleid = 10 查询了一下 id 为 10 的文章的标签,你看哈,还是两个,他们的标签号分别为 2 和 3 。还是两个,没删掉。为什么呢?

找原因

文章更新的方法是 ArticleControlle.php 里的 updat() 方法,对吧?看一下该方法,相比较 store() 好像少了点什么,为什么跟 store() 方法比较,因为他们的过程差不多。在 store() 方法中比 updat() 方法多了一个 tags->attach() 这个过程。我们试试把该语句添加到 update() 方法管用不管用:

public function update($id,ArticleRequest $request){        $article=Article::findOrFail($id);        $article->update($request->all());        $article->tags()->attach($request->input('tag_list'));        return redirect('articles');    } 
Copy after login

再去文章更新的页面修改一下文章的标签,保存之后再查看文章,发现标签多了。。。

其实细想一下,这个 $article 的 tags attach 的是什么?是 input(‘tag_list’) 里面的标签,相当于又把新编辑的对应该文章的标签存到了数据库。不信的话,你再通过 sqlit3 命令去查询一下你刚才编辑的文章的 tag 。肯定变多了。

按照这个思路的话,我们需要把原数据库中存储的 tag 进行删除,然后再添加新的标签,没错,就是这个逻辑。

Laravel 里除了 attach 这个方法外,还有 attach 的反方法 detach ,通过 detach 我们可以删除指定的标签,你可以做个实验,不过 detach 不是我们要讲的。

sync 方法

面对上面的瑕疵,我们的解决思路是先删干净数据库里文章对应的 tag , 然后再给文章添加 tag 。相当于,我们先 detach ,然后再 attach 。

不过, Laravel 还有一个方法叫 sync 。敏感的同学已经能脑补它的全称了,没错 synchronize ,“同步” 的意思。当你把 attach 方法直接修改为 sync ,保存后再编辑文章,修改标签,保存再查看文章,一切都恢复正常了:

public function update($id,ArticleRequest $request){        $article=Article::findOrFail($id);        $article->update($request->all());        $article->tags()->sync($request->input('tag_list'));        return redirect('articles');    } 
Copy after login

所以说, attach 是添加标签,或者绑定标签; detach 是解除标签,或删除标签; sync 是同步标签,或者说是标签的更新。

同理,把 store() 方法里的 attach 也换成 sync 。

总结

今天解决了一个小瑕疵,并对比了三种方法,分别是 attach detach 和 sync 。

attach 就是给文章添加 tag ,绑定 tag ,追加 tag

detach 就是给文章解绑指定的 tag ,删除 tag

sync 就是用来对文章的 tag 进行同步的。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

See all articles