如何在带有标签的帖子和具有相同表格中的类别的帖子之间建立关系

时间:2018-01-16 14:08:31

标签: php mysql wordpress laravel laravel-5

我的数据库中有4个表:postscategoriestagsrelation_with_tag_category

在我的Post模型中,我有'belongsToMany'关系:

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

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

我如何通过上面解释的相同表格与帖子,标签和类别建立关系,如何在我的刀片视图中显示?

就像wordpress术语关系

1 个答案:

答案 0 :(得分:0)

假设您的关系正常工作(我无法判断我是否只看到一个模型关系),您可以使用with函数查询表格。

$myPosts = Post::with(['categories','tags'])->get();

并像这样使用它们

$myPosts->categories->all();

$myPosts->tags->all();

相关问题