比较两个数组Laravel [英] compare two arrays Laravel
问题描述
我的文章可以包含不同的标签.例如,有5个片段:(php,html,css,laravel,js),我也有可以包含不同标签的组.例如4件:(laravel,php,html,css)
I have articles that can contain different tags. For example, 5 pieces: (php, html, css, laravel, js) And I have groups that can also contain different tags. For example 4 pieces: (laravel, php, html, css)
我已经定义了关系并且它可以工作.我仍然缺乏文章与小组的联系.
I have already defined the relationships and it works. What I still lack is the linkage of article to the group.
在Articlecontroller中,我使用了此同步功能:
In the Articlecontroller i use this so sync:
$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
我现在想比较群组的标签和帖子的标签.如果帖子与群组具有相同的标签,我要显示这些帖子.
I would now like to compare the tags of the groups and the tags of the posts. If the posts have the same tags as the group, I want to display the posts.
推荐答案
简单地检查是否相等
$equal = ($tagsFromPost == $tagsFromGroup) //TRUE if both have the same values but NOT in order
$equal = ($tagsFromPost === $tagsFromGroup) //TRUE if both have same values in SAME order
然后类似
if($equal){
<statements>
}
检查是否为假if(!$equal)...
这篇关于比较两个数组Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!