| Install | |
|---|---|
composer require drpshtiwan/livewire-async-select |
|
| Latest Version: | 3.2.0 |
| PHP: | >=8.1 |
A powerful async select component for Laravel Livewire with Alpine.js - a modern, lightweight alternative to Select2.
las- prefixlas-Complete guides, examples, and API reference available at:
To build and view the documentation locally, see DOCS.md.
composer require drpshtiwan/livewire-async-select
php artisan vendor:publish --tag=async-select-assets
<head>
@asyncSelectStyles
{{-- Optional: only if using Bootstrap 4 theme styling --}}
{{-- @asyncSelectBootstrapV4Styles --}}
@livewireStyles
</head>
<body>
{{ $slot }}
@livewireScripts
@stack('scripts') {{-- Required! --}}
</body>
⚠️ Important: The
@stack('scripts')directive is required for the component to work properly.
<livewire:async-select
name="user_id"
wire:model="selectedUser"
endpoint="/api/users/search"
placeholder="Search users..."
/>
<livewire:async-select :options="$users" wire:model="selectedUser">
<livewire:slot name="slot">
<div>{{ $option['label'] }}</div>
</livewire:slot>
<livewire:slot name="selectedSlot">
<strong>{{ $option['label'] }}</strong>
</livewire:slot>
</livewire:async-select>
Slot variables are provided automatically:
$option, $isSelected, $isDisabled, $multiple$optionAPI Route with async-auth middleware:
// Default guard (web)
Route::middleware(['async-auth'])->get('/api/users/search', function (Request $request) {
$users = User::where('name', 'like', "%{$request->get('search')}%")
->limit(20)
->get();
return response()->json(['data' => $users]);
});
// With Sanctum
Route::middleware(['async-auth:sanctum'])->get('/api/users/search', function (Request $request) {
// Works with Sanctum tokens or internal auth
$users = User::where('name', 'like', "%{$request->get('search')}%")
->limit(20)
->get();
return response()->json(['data' => $users]);
});
// With web guard and session persistence
Route::middleware(['web', 'async-auth:web,persist'])->get('/api/users/search', function (Request $request) {
// Persists login in session
$users = User::where('name', 'like', "%{$request->get('search')}%")
->limit(20)
->get();
return response()->json(['data' => $users]);
});
The async-auth middleware is automatically registered and works exactly like auth middleware, but also handles internal authentication automatically when the X-Internal-User header is present. Supports all guards: async-auth:web, async-auth:sanctum, async-auth:api, etc.
| Feature | Livewire Async Select | Select2 |
|---|---|---|
| jQuery Dependency | ❌ No | ✅ Yes |
| Livewire Integration | ✅ Native | ⚠️ Manual |
| Bundle Size | 🟢 Small | 🟡 Large |
| Modern Stack | ✅ Yes | ❌ Legacy |
Contributions are welcome! Please see the documentation for details.
The MIT License (MIT). Please see License File for more information.