SelectFilter
Filters by one value of a fixed list, or with multiple() by several.
use Anvil\Collection\Filters\SelectFilter;
SelectFilter::make('status')->options(['open' => 'Open', 'won' => 'Won', 'lost' => 'Lost']),SelectFilter::make('stage')->options(self::STAGES)->multiple(),SelectFilter::make('customer')->column('customer_id')->relationship(CustomerModel::class, 'name'),Options
Section titled “Options”| Option | Effect |
|---|---|
options([value => label]) |
the choices, in this order |
relationship(Model::class, 'name', $scope) |
the choices are records of another model, see below |
multiple() |
several values can be chosen; a record matches one of them |
Plus label() and column().
Option values must not contain commas (InvalidArgumentException): commas
separate several values.
Control
Section titled “Control”- One value: a select “Status: All” with the options; choosing All removes the filter.
- Several values (
multiple()): a button “Stage: All” that opens a dropdown with checkboxes. It summarises the choice: up to two labels (“Stage: Open, Won”), otherwise the number (“Stage: 3 selected”).
column = value for one value, column IN (…) for several. Values that
are not options are rejected (422); chosen values are applied in the order
of the options.
Records of another model
Section titled “Records of another model”SelectFilter::make('customer')->column('customer_id')->relationship(CustomerModel::class, 'name'),SelectFilter::make('manager')->column('manager_id') ->relationship(PersonModel::class, 'last_name', fn (Model $people) => $people->where('is_manager', 1)),The options are the records of the model by title. The browser sees their
UUIDs; the query uses the internal ids. Which records are offered follows
the rules of the BelongsTo
field: through the resource of the model (tenant, scopeQuery(), view
permission, policies), otherwise the tenant scoped model; a closure
restricts them further. All records are listed, so this suits small and
medium tables.
Saved views, presets and API
Section titled “Saved views, presets and API”The value is the option value ('open') or the UUID of the record, several
separated by commas ('open,won'). API: filter[stage]=open,won.