【问题标题】:compare two arrays Laravel比较两个数组 Laravel
【发布时间】: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


【解决方案1】:

简单地检查是否相等

$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>
}

检查 FALSE if(!$equal)...

【讨论】:

  • 如何使用您的代码加载匹配的文章?
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2012-12-26
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多