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)

4

1 回答 1

1

您可以通过在外部模型中定义本地范围轻松过滤显示的关系选项。例如,如果您只想在关系输入中显示类别的活动条目,请创建Category模型中给定的范围,

public function scopeSubcategories($query){
    return $query->where('parent_id', '!=' , null);
}

现在,转到 BREAD 构建器并将以下内容添加到关系选项中

{
    "scope": "subcategories"
}

该值是您的范围方法的名称,不带范围一词。scopeSubcategories()的值为subcategories

于 2020-06-30T18:26:07.047 回答