模型多对多关联,create时怎么更新中间表数据
文章表
posts:
id,title
中间表:
posts_middle_tags:
id,tag_id,post_id
标签表:
tags:
id,title
在posts模型里定义了多对多关联,怎么在创建文章的时候更新中间表的数据
public function tags(): BelongsToMany
{
return $this->belongsToMany(TagsModel::class, 'posts_middle_tags', 'post_id', 'tag_id')->orderBy('created_at', 'desc');
}
推荐文章: