Laravel - 查询多态关系

时间:2017-07-05 07:40:26

标签: mysql laravel eloquent polymorphic-associations

我有表contenttaxonomiescontent_taxonomiescontent_types。他们的关系设置如下:

内容型号:

public function taxonomies()
{
    return $this->morphMany('App\ContentTaxonomy', 'taxonomical');
}

分类模型:

public function content()
{
    return $this->hasManyThrough('App\Content', 'App\ContentTaxonomy');
}

ContentTaxonomy 型号:

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

public function term()
{
    return $this->hasOne('App\Taxonomy', 'id', 'taxonomy_id');
}

ContentType 型号:

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

分类法表中包含此列的记录示例:

id | tt_id |    slug    |     name
       1     prevensjon    Prevensjon

content_taxonomies 包含带有记录示例的列:

id | taxonomy_id | taxonomical_id | taxonomical_type
           1             1               App\Content

content_types 包含带有记录示例的列:

id | name | slug
     post   post

我有一个带有2个可选参数的路由,例如prevensjon/prosjektsamarbeidets-verdi,其中第一个参数是taxonomy slug,第二个参数是content的slug。 我需要进行查询,在那里我必须找到content content_typepostpage并且属于taxonomy的{​​{1}} slug prevensjonprosjektsamarbeidets-verdi的{​​{1}}相同。 到目前为止,我提取的content content type slug等于post,而content slug等于slug我发送的prosjektsamarbeidets-verdi $content = Content::whereHas('contentType', function ($query) { $query->where('slug', 'post'); }) ->where('slug', $arguments['slug']) ->with('supplementary') ->first();

taxonomy

但是,不确定如何展开此查询,以便我可以进一步检查内容是否slug与我发送的argument相同prevensjonAmazonMwsComplete-master -> AmazonPhpClientLibrary -> MarketplaceWebServiceProducts在这种情况下GetProductCategoriesForSKU

0 个答案:

没有答案
相关问题