I am using Laravel with Voyager for the back-end. I made a relationship between Posts model and Categories model. When adding a new Post, I can choose an according category using a dropdown.
How can I make this dropdown show Categories according to certain conditions? (Let's say, only subcategories)
You can easily filter the shown relationship options by defining a local scope in the foreign model. For example, if you want to only show active entries of categories in a relationship input, create a scope as given in your Category
model,
public function scopeSubcategories($query){
return $query->where('parent_id', '!=' , null);
}
Now, go to the BREAD builder and add the following to the relationship options
{
"scope": "subcategories"
}
The value is the name of your scope-method without the word scope.
The value for scopeSubcategories()
is subcategories
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With