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: getRoleGroupOptionsgetRoleGroupOptions 方法定义。
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);
}