This component requires the Select2 JavaScript library. Make sure to include the Select2 assets in your application.
Basic Usage
Attributes
Inherits all attributes from the Select component, plus:Show clear button to deselect value. Auto-generates placeholder if empty
Hide the search box. Sets
data-minimum-results-for-search="Infinity"Enable tag/token creation. Allows user-entered values
Enable AJAX mode for lazy loading options
URL endpoint for AJAX requests. Required when
is-ajax is trueURL to fetch initially selected items in AJAX mode
Mark as first select in a cascade chain. Adds
data-first="true"CSS selector for child select in cascade. Example:
#stateCSS selector for AJAX child select in cascade
Field name to use as filter parameter in AJAX requests for cascading selects
Enable icon display in options. Adds
select2-b-icon classPrefix for icon class names. Stored in
data-icon-prefix attributeCSS selector for parent modal element. Ensures dropdown renders within modal
Fallback value or behavior
Examples
Basic Select2
data-allow-clear="true"data-placeholder="Nothing Selected"- CSS class:
select2-basic
AJAX Select
- Only initially selected options are loaded
- Additional options load via AJAX as user searches
- The
optionsshould contain only selected values - Uses
name-fieldfor the text field andid-fieldfor the value field
Cascading Selects
- First select triggers changes in child
- Child selects are cleared when parent changes
filter-fieldis sent as parameter in AJAX requests- Use
is-firston the topmost select
With Tags
Hide Search
Disable Clear Button
Icon Select
select2-b-icon class for icon-enabled Select2 templates.
In Modal
Multiple Selection
When
multiple and allow-clear are both true, no placeholder option is rendered in the HTML. Select2 handles the placeholder via JavaScript.With Eloquent Query
Enum Support
Data Attributes
The component sets various data attributes for Select2 initialization:data-allow-clear- Enable clear buttondata-placeholder- Placeholder textdata-minimum-results-for-search- Controls search visibilitydata-tags- Enable tag creationdata-select-ajax-url- AJAX endpoint URLdata-selected-url- URL for fetching selected itemsdata-name-field- Field for option textdata-id-field- Field for option valuedata-filter-field- Filter parameter for cascadingdata-select-child- Child select selectordata-ajax-child- AJAX child selectordata-first- First in cascade chaindata-dropdown-parent-elem- Parent modal selectordata-icon-prefix- Icon class prefixdata-fallback- Fallback value
CSS Classes
The select element includes:form-select- Bootstrap 5 base classselect2-basic- Basic Select2 modeselect2-ajax- AJAX modeselect2-b-icon- Icon modeis-invalid- When validation errors exist
AJAX Response Format
Your AJAX endpoint should return JSON in Select2 format:name-field and id-field:
Default Placeholder
Whenallow-clear is true and no placeholder is provided, the component uses: