[英]Laravel eloquent - making query with optional value key parameters with relationship
我需要使用可選參數數組進行查詢,看起來像這樣:
array:3 [
"parent" => "prosjektsamarbeidets"
"category" => "article"
"slug" => "prosjektsamarbeidets-verdi"
]
它總是有一個slug參數,但其他參數在數組中是可選的。 使用這個參數,我需要在Model Content
進行查詢,在這種情況下, category
的關系在這種情況下是指模型Taxonomy
的建立,如下所示:
public function taxonomies()
{
return $this->morphToMany('App\ContentTaxonomy', 'taggable');
}
parent
鍵值(如果存在)是內容表中記錄的名稱,該表具有以下列:
id | cms_id | title | slug | excerpt | body | parent_id
在通過slug
獲取content
之后,如何進行此類查詢?
$post = Content::where('slug',$arguments['slug'])->get();
if (array_key_exists('parent', $arguments)) {
//do further query by parent name
}
if (array_key_exists('category', $arguments)) {
//do further query by category
}
你應該試試:
public function taxonomies()
{
return $this->morphToMany('App\ContentTaxonomy', 'taggable')->withPivot('additional_attribute', 'additional_attribute');;
}
然后,您可以通過以下方式訪問這些變量:
foreach ($shop->taxonomies as $taxonomy)
{
echo $taxonomy->pivot->additional_attribute;
}
或者你的意思是:
$post = Content::taxonomies()->where('slug',$arguments['slug'])->get();
讓我知道你的意思嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.