【发布时间】:2019-02-20 05:01:52
【问题描述】:
我的文章可以包含不同的标签。例如,5 件:(php, html, css, laravel, js)而且我有组也可以包含不同的标签。比如4块:(laravel, php, html, css)
我已经定义了关系并且它有效。我还欠缺的是文章与群的链接。
在 Articlecontroller 中我使用了这个,所以同步:
$article->groups()->sync($group->id);
文章模型
public function groups()
{
return $this->belongsToMany('App\Group');
}
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
标签模型
public function articles()
{
return $this->morphedByMany('App\Article', 'taggable');
}
public function groups()
{
return $this->morphedByMany('App\Group', 'taggable');
}
组模型
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
public function articles()
{
return $this->belongsToMany('App\Article');
}
控制器
$groups = $user->groups()->latest()->with('tags')->paginate(20);
$mostvotedarticle = Article::where('type', 4)->whereIn('privacy', [1, 3])->orderByVotes()->first(); //show only article with the same tags
$imagearticle = Article::with('comments')->whereIn('type', [4, 5])->where('status', 1)->latest()->paginate(30); //show only articles with the same tags
我现在想比较组的标签和帖子的标签。如果帖子与组具有相同的标签,我想显示帖子。
【问题讨论】:
-
你说帖子是指文章吗?
-
在可标记表中
标签: arrays laravel filter compare