[英]Relationship in Laravel
I have such structure of tables: 我有这样的表结构:
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
And i have have Many To Many Polymorphic Relations. 而且我有多对多态关系。 How from updating post I can update list of tags of this post? 如何从更新帖子中更新帖子标签列表?
Detach all tags from the post, then attach new tags: 从帖子中分离所有标签,然后附加新标签:
$tag_ids = [1, 2, 3...]; $post->tags()->detach(); $post->tags()->attach($tag_ids);
Or detach tag ids no longer needed from the post, then attach new tags: 或者从帖子中分离不再需要的标签ID,然后附加新标签:
$tag_ids_for_detach = [4, 5, 6...]; $tag_ids_for_attach = [1, 2, 3...]; $post->tags()->detach($tag_ids_for_detach); $post->tags()->attach($tag_ids_for_attach);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.