"prosjektsamarbeidets" "category" => "article" "-6ren">
gpt4 book ai didi

php - Laravel eloquent - 使用带有关系的可选值键参数进行查询

转载 作者:行者123 更新时间:2023-11-29 18:42:32 26 4
gpt4 key购买 nike

我需要使用可选参数数组进行查询,可能如下所示:

array:3 [
"parent" => "prosjektsamarbeidets"
"category" => "article"
"slug" => "prosjektsamarbeidets-verdi"
]

它总是有一个 slug 参数,但数组中的其他参数是可选的。有了这个参数,我需要在模型 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();

请告诉我您的意思是不是这个意思?

关于php - Laravel eloquent - 使用带有关系的可选值键参数进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44860234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com