如何处理同一个表中带有标记的文章和带有类别的文章之间的关系

rpppsulh  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(397)

我的数据库中有4个表: posts , categories , tags 以及 relation_with_tag_category 在我的后模型中,我有一种“归属感”关系:

public function categories(){
    return $this->belongsToMany('App\Category', 'category_relation')->withTimestamps();
}

public function tags(){
    return $this->belongsToMany('App\Tag', 'category_relation')->withTimestamps();
}

如何通过我上面解释的同一个表与post、tag和category建立关系,以及如何在我的blade视图中显示?
就像wordpress术语关系

ghhaqwfi

ghhaqwfi1#

假设您的关系工作正常(我无法判断是否只看到一个模型关系),您可以使用with函数查询表。 $myPosts = Post::with(['categories','tags'])->get(); 像这样使用它们
$myPosts->categories->all(); $myPosts->tags->all();

相关问题