ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] tags 标签 | 键名 | 类型 | | --- | --- | | id | PK | | keywords | varchar(200) | | taggables | 多对多关联 | taggables 多对多关联 | 键名 | 类型 | | --- | --- | | id | PK | | taggable_type | varchar(200) | | taggable_id | integer | | tag_id | integer FK | # 实现 Post.php ~~~ function tags() { return $this->morphToMany('App\\Tag', 'taggable'); } ~~~ Tag.php ~~~ function posts() { return $this->morphedByMany('App\Post', 'taggable'); } ~~~ # 操作 `$post = Post::find(1);` 绑定Tag:['iOS', 'iPhone'] ~~~ $id_list = []; foreach (['iOS', 'iPhone'] as $keywords) $id_list[] = Tag::firstOrCreate(['keywords' => $keywords])->getKey(); $post->sync($id_list); ~~~ 用法与多对多相同