Jul 1, 2017
0
Level 4
Laravel eloquent - making query with optional value key parameters with relationship
I need to make a query with optional parameters array, that could look something like this:
array:3 [
"parent" => "prosjektsamarbeidets"
"category" => "article"
"slug" => "prosjektsamarbeidets-verdi"
]
It will always have a slug parameter but other parameters are optional in the array. With this arguments I need to make a query in the Model Content, where the relationship for the category which refers in this case to the model Taxonomy is set up like this:
public function taxonomies()
{
return $this->morphToMany('App\ContentTaxonomy', 'taggable');
}
And the parent key value if it exists is the name of the record from the contents table, which has this columns:
id | cms_id | title | slug | excerpt | body | parent_id
How can I do such a query, after I get the content by slug?
$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
}
Please or to participate in this conversation.