作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
"prosjektsamarbeidets" "category" => "article" "-6ren">
我需要使用可选参数数组进行查询,可能如下所示:
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/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!