Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Voyager: Dropdown that shows conditional relations

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)

like image 750
16 revs Avatar asked Nov 08 '22 13:11

16 revs


1 Answers

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.

like image 84
Kiran Maniya Avatar answered Nov 15 '22 05:11

Kiran Maniya