| Install | |
|---|---|
composer require fabioguin/livewire-searchable-select |
|
| Latest Version: | 2.0.1 |
| PHP: | ^8.1|^8.2 |
High-performance Livewire component for searchable select inputs with relevance-based ordering, intelligent caching, and optimized UX.
You can install the package via composer:
composer require fabioguin/livewire-searchable-select
For optimal performance, configure Redis as your cache driver:
# In your .env file
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
The package will automatically use Redis for caching search results, significantly improving performance.
SearchableSelect in your livewire component:<?php
namespace App\Http\Livewire\CreateUser;
use Livewire\Component;
use FabioGuin\LivewireSearchableSelect\Traits\SearchableSelect;
class CreateUser extends Component
{
use SearchableSelect;
// set properties to get selected value from LivewireSearchableSelect
public int $country_id;
}
livewire-searchable-select component in your blade view, and pass in a parameters:
<livewire:select-searchable-input
property="country_id"
model-app="\App\Models\Country"
model-app-scope="isActive"
option-text="{name}"
option-value-column="id"
active-option-text="{{ request()->user()->country_name }}"
active-option-value="{{ request()->user()->country_id }}"
:search-columns="['name']"
:search-min-chars="2"
:search-limit-results="15"
input-extra-classes="mt-3"
input-placeholder="Select country" />
The component automatically caches search results for 5 minutes, dramatically reducing database load:
// Results are automatically cached with intelligent cache keys
// Cache duration: 5 minutes (configurable)
// Cache driver: Uses your configured cache driver (Redis recommended)
Search results are intelligently ordered by relevance:
Input is debounced by 300ms to prevent excessive server requests:
<!-- Automatically debounced - no configuration needed -->
<livewire:select-searchable-input ... />
| Property | Arguments | Result | Example |
|---|---|---|---|
| property | String - required property name | Define the property name | property="country_id" |
| model-app | String - required full model name | Define the source of data that will be select | model-app="\App\Models\Country" |
| model-app-scope | String - optional name of model sope | Define model scope for filtering results | model-scope-app="isActive" |
| option-text | String - required show column on option | Define the column(s) in model that want to be show in select option | option-text="{id} - {name} ({abbreviation})" |
| option-value-column | String - required set value | Define the column name as a value data that will be selected | option-value-column="id" |
| active-option-text | Mixed - optional set active value text | Define the default selected option to show on select | active-option-text="{{ request()->user()->country_name }}" |
| active-option-value | Mixed - optional set active value | Define the default selected option value to pass in the model | active-option-value="{{ request()->user()->country_id }}" |
| search-columns | Array - required search column | Define the column in model that want to be searched | :search-columns="['name', 'abbreviation']" |
| search-min-chars | Int - optional minimum character | Define minimum character for trigger search event; default: 0 | :search-min-chars="2" |
| search-limit-results | Int - optional max results to view in the dropdown | Define the lenght of result for dropdown; default: 10 | :search-limit-results="15" |
| input-extra-classes | String - optional add extra classes | Define the extra classes for the input, anyway each element has a class without defined attributes that can be exploited for customization, for example: "select-searchable-input", "select-searchable-input-clear-value", etc. | input-extra-classes="mt-3" |
| input-placeholder | String - optional placeholder name | Define the placeholder for select input | input-placeholder="Select country" |
With this parameter you can define a query scope of the model to filter the search results in a complex way (see official Laravel documentation). This makes the component even more flexible and usable in multiple contexts. Remember to use a string with the camel case syntax without specifying that it is a "scope" (see example).
Livewire Select is designed to be easily customizable. You can publish and modify the configuration, views, and language files to suit your needs.
You can publish the configuration file with:
php artisan vendor:publish --provider="FabioGuin\LivewireSearchableSelect\LivewireSearchableSelectServiceProvider" --tag="config"
This will publish a livewire-searchable-select.php config file to your config directory. Here you can change the default settings of Livewire Select.
If you need to modify the views, you can publish them with:
php artisan vendor:publish --provider="FabioGuin\LivewireSearchableSelect\LivewireSearchableSelectServiceProvider" --tag="views"
This will publish the view files to resources/views/vendor/livewire-searchable-select. You can edit these files to change the appearance of the select input.
To customize the language strings, you can publish the language files with:
php artisan vendor:publish --provider="FabioGuin\LivewireSearchableSelect\LivewireSearchableSelectServiceProvider" --tag="lang"
This will publish the language files to resources/lang/vendor/livewire-searchable-select. You can edit these files to change the text used by Livewire Select.
SearchableSelect trait moved to FabioGuin\LivewireSearchableSelect\Traits\SearchableSelectwire:model with debounced Alpine.js input// Before (v1.x)
use FabioGuin\LivewireSearchableSelect\SearchableSelect;
// After (v2.0.0)
use FabioGuin\LivewireSearchableSelect\Traits\SearchableSelect;
The component API remains the same, so your existing Blade templates will continue to work without modification.
For better performance, configure Redis as your cache driver (see Installation section above).
The package is actively maintained with the following planned features:
Inspired by https://github.com/mitratek/livewire-select
We ❤️ Semantic Versioning https://semver.org/
Open to work, contact me: https://www.linkedin.com/in/fabio-guin-starzero/