Tag List
The taglist field picks several values as tags, typed in or chosen from a list of options. It saves them as a JSON array, as one joined string, or as related records.
Modes
mode sets how the tags are saved, and when it is not set it follows the attribute:
- a relation uses
relationmode, which saves the related records' keys; - a JSON attribute uses
arraymode, which saves a JSON array; - anything else uses
stringmode, which saves one string with the tags joined byseparator.
A JSON attribute has an array cast. See JSON attributes.
/**
* casts stores the structured fields as JSON through array casts, and reads the switch and checkbox columns as booleans.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'features' => 'array',
'permissions' => 'array',
'interests' => 'array',
'tags' => 'array',
'topics' => 'array',
'hashtags' => 'array',
'content' => 'array',
'steps' => 'array',
'address' => 'array',
'line_items' => 'array',
'meta' => 'array',
'shipping' => 'array',
'contact' => 'array',
'faqs' => 'array',
'highlights' => 'array',
'milestones' => 'array',
'dimensions' => 'array',
'sections' => 'array',
'specs' => 'array',
'itinerary' => 'array',
'languages' => 'array',
'deliveries' => 'array',
'variants' => 'array',
'stockists' => 'array',
'parts' => 'array',
'price_breaks' => 'array',
'bundle_items' => 'array',
'suppliers' => 'array',
'components' => 'array',
'stock_levels' => 'array',
'reviews' => 'array',
'spec_sheet' => 'array',
'certifications' => 'array',
'awards' => 'array',
'seo' => 'array',
'packaging' => 'array',
'is_featured' => 'boolean',
'has_discount' => 'boolean',
];
}Array mode
mode: array saves the tags as a JSON array, such as ["red", "blue"]. tags already has an array cast, so the mode would follow anyway, and setting it makes the choice explicit. customTags lets the user type new tags, each ended with Enter or the separator, a comma by default. placeholder fills the field while it is empty.
fields:
tags:
label: Tags
type: taglist
mode: array
customTags: true
placeholder: Type a tag and press Enter
comment: Typed tags saved as a JSON array.String mode
String mode joins the tags into one value with separator, either comma (the default) or space, and splits the saved value the same way when the form loads. The separator also ends a typed tag, so here a space finishes each keyword.
fields:
keywords:
label: Keywords
type: taglist
mode: string
separator: space
customTags: true
placeholder: Type keywords
comment: Typed tags saved as one string, separated by spaces.Fixed options
options limits the tags to a fixed list, saved by their keys, such as ["design", "research"]. Like a dropdown's options, it takes an inline list, the name of a model method that returns them, or true to call the model's get{Field}Options() method. maxItems caps how many tags can be picked.
fields:
topics:
label: Topics
type: taglist
maxItems: 3
placeholder: Pick up to three topics
options:
design: Design
engineering: Engineering
marketing: Marketing
research: Research
support: Support
comment: Fixed options saved by their keys, three at most.Sorting and checking tags
sortable lets the user drag the tags into a new order, and the field saves them in that order, which matters in array and string mode. With customTags, tagPattern is a regular expression a typed tag must match before it becomes a tag, here lowercase words joined by dashes, and createOnBlur keeps the text still being typed as a tag when the field loses focus, rather than dropping it. The pattern only guides the user, so validate the saved value with the model's rules as well.
fields:
hashtags:
label: Hashtags
type: taglist
mode: array
customTags: true
sortable: true
tagPattern: ^[a-z0-9-]+$
createOnBlur: true
placeholder: Type lowercase tags, drag to reorder
comment: Lowercase tags with dashes, kept in the order they are dragged to.Saving labels
useKey: false saves the option labels instead of their keys, here ["English", "Japanese"]. Custom tags always save the text that was typed, so customTags turns useKey off.
fields:
languages:
label: Languages
type: taglist
useKey: false
placeholder: Pick languages
options:
en: English
fr: French
ja: Japanese
pt: Portuguese
comment: Fixed options saved by their labels instead of their keys.Relation mode
On a relation, such as this belongsToMany relation to UserGroup, the options are every record of the related model, named by its nameFrom column, and saving syncs the relation to the picked records.
fields:
groups:
label: Groups
type: taglist
nameFrom: name
placeholder: Add groups
comment: Picks from every group and saves the relation./**
* groups are the groups picked by the checkbox list and the tag list.
*/
public function groups(): BelongsToMany
{
return $this->belongsToMany(UserGroup::class, 'sample_user_group');
}
/**
* categories are the categories picked by the tree checkbox list.
*/
public function categories(): BelongsToMany
{
return $this->belongsToMany(Category::class, 'category_sample');
}
/**
* labels are the tags picked or created by the tag list.
*/
public function labels(): BelongsToMany
{
return $this->belongsToMany(Tag::class, 'sample_tag');
}The field posts the keys of the picked records under the relation's name, so sync the relation in your save handler, as the relation field shows.
Creating related records
With customTags in relation mode, the field matches tags to records by their nameFrom column, and a typed tag that matches no record creates one, setting only that column, so the related model's other columns must accept empty values. When the field also sets options, it cannot create records, and saving a tag that matches none of the options throws an error.
fields:
labels:
label: Labels
type: taglist
nameFrom: name
customTags: true
placeholder: Pick a tag or type a new one
comment: A typed tag that does not exist yet is created as a new tag record.Read-only
readOnly lists the tags without an input, and still posts them from hidden inputs. This copy reads the tags through valueFrom, and its name starts with an underscore, which keeps it out of the saved data.
fields:
_tags_readonly:
label: Tags
type: taglist
valueFrom: tags
readOnly: true
customTags: true
comment: Lists the tags without an input, and still posts them.Preview
disabled lists the picked labels in preview mode, and the field is left out of the save.
fields:
topics_preview:
label: Topics
type: taglist
valueFrom: topics
disabled: true
options:
design: Design
engineering: Engineering
marketing: Marketing
research: Research
support: Support
comment: Lists the picked topics as a preview, which is never saved.Options
| Option | Description |
|---|---|
| mode | how the tags are saved: array, string or relation. Default: follows the attribute. |
| separator | the character that joins the tags in string mode and ends a typed tag: comma or space. Default: comma. |
| customTags | lets the user type new tags. In relation mode a new tag creates a record. Default: false. |
| options | a fixed list of tags, the name of a model method that returns them, or true for get{Field}Options(). Default: the related records in relation mode, otherwise none. |
| useKey | saves the option keys rather than their labels. Turned off by customTags. Default: true. |
| nameFrom | in relation mode, the related model's column that names each tag. Default: name. |
| placeholder | the text shown while the field is empty. Default: none. |
| maxItems | the most tags that can be picked. Default: no limit. |
| sortable | lets the user drag the tags into a new order, saved in that order. Default: false. |
| tagPattern | with customTags, a regular expression a typed tag must match, such as ^[a-z0-9-]+$. Default: none. |
| createOnBlur | with customTags, keeps the typed text as a tag when the field loses focus. Default: false. |
The field also takes the common field properties, such as readOnly, disabled and valueFrom.