Laravel eloquent - 用数组约束关系查询

时间:2017-10-25 09:59:32

标签: laravel eloquent

我有3个模型ContentContentTypeTaxonomy。他们的关系设置如下:

内容型号:

public function contentType()
{
    return $this->belongsTo('App\ContentType', 'ct_id');
}
public function taxonomies()
{
    return $this->morphToMany('App\Taxonomy', 'taxonomical');
}

ContentType 型号:

public function contents()
{
    return $this->hasMany('App\Content', 'ct_id', 'id');
}

分类

public function contents()
{
    return $this->morphedByMany('App\Content', 'taxonomical');
}

我得到一个带有分类法的数组,我应该用它来过滤内容,数组看起来像这样:

$taxonomiesArray =  ["Administrasjon", "Oslo", "Bane"]

使用这种数组我应该进行查询,我只得到contents,其中包含该数组中的所有taxonomies,并且是contentType,其中列名是intranet-post,基本上是这样的:

$intranetTypePostID = ContentType::where('name', 'intranet-post')->pluck('id');
$contents = Content::where('ct_id', $contentType->id)->with(['taxonomies' => function($q) use ($userTaxonomies){
      $q->whereIn('slug', $userTaxonomies);
    }])->get();

但是,我怎样才能获得所有taxonomies具有与数组$taxonomiesArray中的值相同的slug的内容?

0 个答案:

没有答案