5

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)

1 Answers1

2

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.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81