Group Scope

Group Scope

Filter Scope

group - filter using a group of multiple items, usually by a related model or an array of predefined options.

To filter by a model, specify the modelClass and nameFrom properties to specify which model and attribute to use.

roles:
    label: Role
    type: group
    nameFrom: name
    modelClass: October\Test\Models\Role

The following properties are available for the filter.

PropertyDescription
optionsavailable options for the filter, as an array.
optionsMethodtake options from a method defined on the model or as a static method, eg Class::method.
optionsScopeapplies a model scope method to the options query.
conditionsa custom SQL select statement to use for the filter.
nameFromthe column name to use in the model class, used for displaying the name. Default: name.
modelClassclass of the model to use for the available filter records.
modelScopeapplies a model scope method to the filter query.
matchModedetermines how the selection should be applied, either include, exclude or toggle. Default: include

To filter by an array, specify an options property.

status:
    label: Role
    type: group
    options:
        developer: Developer
        publisher: Publisher

You may pass custom SQL to the conditions as a string where :value contains the filtered value.

status:
    label: Role
    type: group
    conditions: role in (:value)
    # ...

You may also pass a default value as an array with selected keys.

status:
    # ...
    default:
        - developer
        - publisher

Use the matchMode property control how the filter is applied, either by including or excluding the selected items.

status:
    # ...
    matchMode: toggle

# PHP Interface

You may define a custom modelScope in the model using the following example.

roles:
    label: Role
    type: group
    nameFrom: name
    modelClass: October\Test\Models\Role
    modelScope: groupFilter

The scopeGroupFilter method definition where the value is found in $scope->value.

public function scopeGroupFilter($query, $scope)
{
    return $query->whereHas('roles', function($q) use ($scope) {
        $q->whereIn('id', $scope->value);
    });
}

You may dynamically supply options by passing a model method to the optionsMethod property.

roles:
    label: Role
    type: group
    nameFrom: name
    modelClass: October\Test\Models\Role
    optionsMethod: getRoleGroupOptions

The getRoleGroupOptions method definition.

public function getRoleGroupOptions()
{
    return $this->whereNull('parent_id')->pluck('name', 'id')->all();
}

The optionsScope property allows you to apply a scope to the default query that locates the available options.

roles:
    label: Role
    type: group
    nameFrom: name
    modelClass: October\Test\Models\Role
    optionsScope: applyRoleOptionsFilter
public function scopeApplyRoleOptionsFilter($query)
{
    return $query->where('id', '<>', 1);
}
On This Page