企业🤖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); ~~~ 用法与多对多相同