group - 使用一组多个项目进行过滤,通常通过相关模型或预定义选项数组。
要按模型过滤,指定 modelClass 和 nameFrom 属性以指定要使用的模型和属性。
roles:
label: Role
type: group
nameFrom: name
modelClass: October\Test\Models\Role以下属性可用于过滤器。
| Property | Description |
|---|---|
| options | available options for the filter, as an array. |
| optionsMethod | take options from a method defined on the model or as a static method, eg Class::method. |
| optionsScope | applies a model scope method to the options query. |
| conditions | a custom SQL select statement to use for the filter. |
| nameFrom | the column name to use in the model class, used for displaying the name. Default: name. |
| modelClass | class of the model to use for the available filter records. |
| modelScope | applies a model scope method to the filter query. |
| matchMode | determines how the selection should be applied, either include, exclude or toggle. Default: include |
要按数组筛选,指定一个 options 属性。
status:
label: Role
type: group
options:
developer: Developer
publisher: Publisher您可以将自定义 SQL 作为 :value 包含过滤后值的字符串传递给条件。
status:
label: Role
type: group
conditions: role in (:value)
# ...你还可以传递一个 default 值,作为一个包含选定键的数组。
status:
# ...
default:
- developer
- publisher使用 matchMode 属性控制筛选器如何应用,即通过包含或排除选定项。
status:
# ...
matchMode: toggle您可以使用以下示例在模型中定义一个自定义的modelScope。
roles:
label: Role
type: group
nameFrom: name
modelClass: October\Test\Models\Role
modelScope: groupFilter该 scopeGroupFilter 方法定义 其中值位于 $scope->value。
public function scopeGroupFilter($query, $scope)
{
return $query->whereHas('roles', function($q) use ($scope) {
$q->whereIn('id', $scope->value);
});
}您可以动态地提供选项,方法是将模型方法传递给 optionsMethod 属性。
roles:
label: Role
type: group
nameFrom: name
modelClass: October\Test\Models\Role
optionsMethod: getRoleGroupOptions该 getRoleGroupOptions 方法定义。
public function getRoleGroupOptions()
{
return $this->whereNull('parent_id')->pluck('name', 'id')->all();
}optionsScope 属性允许您对用于定位可用选项的默认查询应用一个作用域。
roles:
label: Role
type: group
nameFrom: name
modelClass: October\Test\Models\Role
optionsScope: applyRoleOptionsFilterpublic function scopeApplyRoleOptionsFilter($query)
{
return $query->where('id', `<>`, 1);
}