Scope Types
The Filter widget is a bar of scopes above a list, each shown as a chip that narrows the list. A chip toggles, opens a list of options, or opens a popover with a small form. An active chip shows its value and a button that clears it, and keeps its place in the bar. bindToListWidget() applies the active scopes to the list query and refreshes the list when one changes. Each scope remembers its value for the session, and the menu at the start of the bar, whose icon counts the active chips, clears them all or saves them as a named set to apply again. Adding a filter to a page is covered in Building a List Page.
The filters on this page
Each example binds a filter, with the scopes from its own YAML file, to a list of the seeded posts. The lists share one columns file.
<?php
namespace App\Controllers\Reference\FiltersController;
use App\Models\Comment;
use App\Models\Post;
use Larajax\Ui\Widgets\Filter;
use Larajax\Ui\Widgets\Lists;
/**
* ScopesExamples builds the filters on the Scope Types page, each bound to its own list of posts.
*/
trait ScopesExamples
{
/**
* makeScopeExamples builds a filter for each scope type example, with the scopes from its YAML file, bound to a list of posts.
*
* @return array<string, array{filter: Filter, list: Lists}>
*/
protected function makeScopeExamples(): array
{
$examples = [];
foreach (['checkbox', 'switch', 'dropdown', 'group', 'date', 'text', 'number'] as $name) {
$list = Lists::make(
alias: $name.'List',
model: new Post,
columns: '~/resources/ui/reference/filters/posts.yaml',
recordsPerPage: 5,
noRecordsMessage: 'No posts match the filter.',
customPageName: false,
);
$filter = Filter::make(
alias: $name.'Filter',
model: new Post,
scopes: "~/resources/ui/reference/filters/scopes/{$name}.yaml",
);
$filter->bindToListWidget($list);
$examples[$name] = ['filter' => $filter, 'list' => $list];
}
$examples['trashed'] = $this->makeTrashedExample();
return $examples;
}
// #region docs-trashed
/**
* makeTrashedExample builds a filter that shows the soft deleted comments, bound to a list of comments.
*
* @return array{filter: Filter, list: Lists}
*/
protected function makeTrashedExample(): array
{
$list = Lists::make(
alias: 'trashedList',
model: new Comment,
columns: '~/resources/ui/reference/filters/comments.yaml',
recordsPerPage: 5,
noRecordsMessage: 'No comments match the filter.',
customPageName: false,
);
$filter = Filter::make(
alias: 'trashedFilter',
model: new Comment,
scopes: '~/resources/ui/reference/filters/scopes/trashed.yaml',
);
$filter->bindToListWidget($list);
return ['filter' => $filter, 'list' => $list];
}
// #endregion docs-trashed
}columns:
title:
label: Title
searchable: true
category:
label: Category
relation: category
select: name
tags:
label: Tags
type: partial
path: ~/resources/views/reference/lists/columns/_column_tags.php
relationWith: tags
sortable: false
status:
label: Status
type: selectable
published:
label: Live
type: switch
views:
label: Views
type: number
comments_count:
label: Comments
type: number
relation: comments
relationCount: true
published_at:
label: Published
type: date
format: j M Y
review_on:
label: Review
type: date
format: j M YCheckbox
checkbox is on or off, and a click on its chip toggles it, with a check mark in place of the plus while it is on. Off, it applies nothing. On, it keeps the records whose column of the same name is true, like published, or the records that match the SQL in conditions, like the posts with comments here.
scopes:
published:
label: Published
type: checkbox
commented:
label: Has comments
type: checkbox
conditions: exists (select 1 from comments where comments.post_id = posts.id)Switch
switch has three states, picked from its chip's menu like a dropdown: Any applies nothing, a check mark applies the second of its two conditions and a cross the first. Without conditions, the check keeps the records whose column of the same name is true and the cross those where it is false. The chip shows the check or cross beside its label, and screen readers announce it as yes or no.
scopes:
published:
label: Published
type: switch
conditions:
- published = false
- published = trueDropdown
dropdown picks one value for the column of the same name, applied as soon as an option is picked from its chip. Its options come from options in the YAML, or from optionsMethod, given here as a static method, Class::method, whose options carry colors. An option is written as value: label, or as value: [label, icon] or value: [label, color] like the options of a dropdown field. A color is a value starting with #, var(, rgb(, rgba( or hsl(, and shows as a status dot before the label. emptyOption is the option that applies no filter.
A static options method is called with the filter's model and the scope, and a plain method name is called on the filter's model with the scope.
A long list gets a search box at the top of its options, which filters them as you type, ignoring case and accents. Enter picks the first option that matches. search: true always shows the box, search: false never does, and without the setting it shows once there are more than ten options, as for the categories here.
scopes:
status:
label: Status
type: dropdown
emptyOption: Any status
optionsMethod: App\Models\Post::listStatusColorOptions
category:
label: Category
type: dropdown
emptyOption: Any category
optionsMethod: App\Models\Post::listCategoryOptions
conditions: category_id = :value/**
* listStatusColorOptions returns the workflow statuses as a label and color pair, which a dropdown shows as a status dot.
*
* @return array<string, array{0: string, 1: string}>
*/
public static function listStatusColorOptions(): array
{
return [
'draft' => ['Draft', 'var(--bs-orange-500)'],
'review' => ['In Review', 'var(--bs-indigo-500)'],
'approved' => ['Approved', 'var(--bs-green-500)'],
'archived' => ['Archived', 'var(--bs-gray-400)'],
];
}/**
* listCategoryOptions returns every category by name, a list long enough for its dropdown filter to offer a search box.
*
* @return array<int, string>
*/
public static function listCategoryOptions(): array
{
return Category::query()->orderBy('name')->pluck('name', 'id')->all();
}Group
group opens a popover with a searchable list of options, to pick several, and the chip shows how many are picked, after a cross while it excludes them. When the scope is named after a relation of the model, like category, the options are the related records, labelled by their nameFrom attribute, and the filter keeps the records related to any picked one. The popover lists the first 200 related records, and its search box looks up the rest. The search box takes the focus when the popover opens, so typing starts a search, except on touch devices, where the on-screen keyboard would cover the options.
With options or optionsMethod in place of a relation, the filter keeps the records whose column holds any picked value, or, for a column the model stores as JSON, contains one.
scopes:
category:
label: Category
type: group
nameFrom: name
status:
label: Status
type: group
options:
draft: Draft
review: In Review
approved: Approved
archived: ArchivedDate
date compares the column with dates, and the popover offers:
- is in the last and is in the next, a number of days, weeks, months or years counted from today, today included, so the last 7 days are today and the six before. The scope keeps the period rather than its dates, so a saved filter moves with the days.
- is on, any time on the picked day.
- is between, is on or after and is before or on, where each picked day counts whole.
- is empty and is not empty, the records without or with a date.
Days are read in the viewer's timezone. daterange offers only between, and leaving one end of the range empty turns it into on or after, or before or on.
scopes:
published_at:
label: Published
type: date
review_on:
label: Review Date
type: daterangeText
text matches the column against typed text, either as the whole value (is equal to) or as a part of it (contains). Is empty matches the records with no text, missing or blank, and is not empty the others.
scopes:
title:
label: Title
type: textNumber
number compares the column with a number: equal to, greater than, less than, or between two numbers, where greater, less and between include the numbers given, and is empty or is not empty matches the records without or with a number. numberrange offers only between, and leaving one end empty turns it into greater or less.
scopes:
views:
label: Views
type: number
id:
label: ID
type: numberrangeTrashed
trashed shows the soft deleted records, which the model's SoftDeletes trait otherwise leaves out. Its menu picks Shown, for the deleted records alongside the others, or Only, for the deleted records alone, while Hidden, the default, filters nothing. The list gives a deleted record's row the deleted class, which greys it out. The scope needs a model that uses soft deletes, and restoring a record is left to the page, such as a toolbar button that calls restore() on the checked records.
scopes:
deleted:
label: Deleted
type: trashed/**
* makeTrashedExample builds a filter that shows the soft deleted comments, bound to a list of comments.
*
* @return array{filter: Filter, list: Lists}
*/
protected function makeTrashedExample(): array
{
$list = Lists::make(
alias: 'trashedList',
model: new Comment,
columns: '~/resources/ui/reference/filters/comments.yaml',
recordsPerPage: 5,
noRecordsMessage: 'No comments match the filter.',
customPageName: false,
);
$filter = Filter::make(
alias: 'trashedFilter',
model: new Comment,
scopes: '~/resources/ui/reference/filters/scopes/trashed.yaml',
);
$filter->bindToListWidget($list);
return ['filter' => $filter, 'list' => $list];
}columns:
author:
label: Author
content:
label: Comment
post:
label: Post
relation: post
select: title
created_at:
label: Posted
type: date
format: j M YScope properties
Every scope takes these properties, where they apply.
| Property | Description |
|---|---|
| label | the scope's name in the bar. |
| shortLabel | a shorter name for the bar, in place of label. |
| type | the scope type. Default: group. |
| valueFrom | the column, or relation for a group, that the scope filters. Default: the scope name. |
| conditions | raw SQL in place of the default query. See conditions. |
| modelScope | a model scope, or a static Class::method, in place of the default query, receiving the query and the scope with its value. See model scopes. |
| default | the value the scope starts with, and returns to when the filter is cleared. See default values. |
| options | the options of a dropdown or a group. |
| optionsMethod | a model method, or a static Class::method, that returns the options. |
| emptyOption | the label of an extra option: on a dropdown it applies no filter, and on a group it matches the records with no value. |
| search | on a dropdown, true shows a search box above the options and false hides it. Default: shown for more than ten options. |
| nameFrom | the attribute that labels a group's options from a relation. Default: name. |
| modelClass | the model that supplies a group's options and answers its options method, in place of the relation named by the scope. |
| dependsOn | a scope name, or an array of them. When one of them changes, this scope is cleared and rendered again. |
| permissions | gate abilities the visitor needs to see the scope, as a string or an array where any one is enough, checked with Laravel's Gate. |
| context | the filter context, or an array of them, that shows the scope. A scope named name@context does the same. |
| cssClass | a class added to the scope. |
| order | a number for the scope's position. Default: 100, 200 and so on, in the order of the definitions. |
| after | places the scope after the named scope. |
| before | places the scope before the named scope. |
Scope types
| Type | Shows | Options |
|---|---|---|
| checkbox | a checkbox, on or off. | conditions, modelScope |
| switch | a checkbox with three states: off, the first condition and the second. | conditions, modelScope |
| dropdown | a select of options, one picked. | options, optionsMethod, emptyOption, search, conditions, modelScope |
| group | a popover with a searchable list, several picked. | options, optionsMethod, nameFrom, modelClass, optionsScope, emptyOption, matchMode, conditions, modelScope |
| date | a popover comparing with a period or a picked date. | conditions, minDate, maxDate, yearRange, firstDay, showWeekNumber, useTimezone, modelScope |
| daterange | a date scope offering only between. | as date |
| text | a popover matching typed text. | conditions, modelScope |
| number | a popover comparing with a number. | conditions, format, prefix, suffix, prefixIcon, suffixIcon, modelScope |
| numberrange | a number scope offering only between. | as number |
| trashed | a menu that shows the soft deleted records alongside the others or alone. | modelScope |
Type options
| Option | Description |
|---|---|
| conditions | checkbox: SQL applied while it is on. switch: a list of two SQL statements, one for each state. dropdown: SQL with :value for the picked option. group: SQL with :value for the picked values, quoted and separated by commas, as in status in (:value). date, text and number: the choices offered, keyed by name, each true for the default query or SQL of its own, as listed under conditions. |
| optionsScope | group: a scope of the options model that narrows the options. |
| matchMode | group: toggle lets the visitor choose between including and excluding the picked options, and include or exclude fixes the mode. Default: include. |
| minDate | date: the first date that can be picked. Default: 1970-01-01. |
| maxDate | date: the last date that can be picked. Default: 2199-12-31. |
| yearRange | date: how many years either side the calendar's year view offers. Default: 10. |
| firstDay | date: the first day of the week, from 0 for Sunday. Default: the locale's. |
| showWeekNumber | date: numbers the weeks in the calendar. Default: false. |
| useTimezone | date: reads the picked days in the viewer's timezone. false compares them with the stored dates as they are. Default: true. |
| format | number: shows thousands separators in the inputs and the chip while the plain number posts, as true or an array of decimals, grouping and locale. See number options. |
| prefix | number: text shown inside the inputs before the number, and before it in the chip. |
| suffix | number: text shown inside the inputs after the number, and after it in the chip. |
| prefixIcon | number: icon classes shown inside the inputs before the number. |
| suffixIcon | number: icon classes shown inside the inputs after the number. |
Filter widget
Filter::make() takes these options as named arguments.
| Option | Description |
|---|---|
| model | the model the scopes filter, which also supplies relation options and options methods. |
| scopes | the scope definitions, as a YAML file path or an array. |
| context | the filter's context, which shows only the scopes for that context. |
| alias | the widget's name, unique on the page. |
| search | a search box after the scopes, as a search widget configuration, a partial name or a Search widget. See search and list setup. |
| setupHandler | the handler that opens the list setup, usually $list->getEventHandler('onLoadSetup'), shown as an icon after the scopes. |
| primary | a row above the scopes: search for the search box and list setup, or the name of a dropdown scope, shown as tabs. Default: none, one row. |
| savedFilters | lets the menu save the scope values under a name and apply them again, see saved filters. Default: true. |
bindToListWidget($list) applies the active scopes to the list query, refreshes the list when a scope changes, and shares the list's page parameter, so the list goes back to its first page. With search, it also searches the list and keeps the search term for the session, as a toolbar search does.