1. Go to this page and download the library: Download williamug/searchable-select library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
williamug / searchable-select example snippets
namespace App\Livewire;
use App\Models\Country;
use Livewire\Component;
class ContactForm extends Component
{
public $countries;
public $country_id;
public function mount()
{
// Load all countries
$this->countries = Country::orderBy('name')->get();
}
public function save()
{
$this->validate([
'country_id' => '
// If your model has 'code' and 'country_name' fields
$countries = Country::all(); // [['code' => 'US', 'country_name' => 'United States'], ...]
namespace App\Livewire;
use App\Models\Country;
use Livewire\Component;
class UserProfile extends Component
{
public $countries;
public $country_id;
public function mount()
{
$this->countries = Country::orderBy('name')->get();
}
public function render()
{
return view('livewire.user-profile');
}
}
namespace App\Livewire;
use App\Models\Skill;
use Livewire\Component;
class UserSkills extends Component
{
public $skills;
public $selected_skills = []; // Array to hold multiple selections
public function mount()
{
$this->skills = Skill::orderBy('name')->get();
}
public function render()
{
return view('livewire.user-skills');
}
}
namespace App\Livewire;
use App\Models\{Country, Region, City};
use Livewire\Component;
class LocationSelector extends Component
{
// Options
public $countries;
public $regions = [];
public $cities = [];
// Selected values
public $country_id;
public $region_id;
public $city_id;
public function mount()
{
// Load countries on page load
$this->countries = Country::orderBy('name')->get();
}
public function updatedCountryId($value)
{
// When country changes, load its regions
$this->regions = Region::where('country_id', $value)
->orderBy('name')
->get();
// Reset child selections
$this->region_id = null;
$this->city_id = null;
$this->cities = [];
}
public function updatedRegionId($value)
{
// When region changes, load its cities
$this->cities = City::where('region_id', $value)
->orderBy('name')
->get();
// Reset city selection
$this->city_id = null;
}
public function render()
{
return view('livewire.location-selector');
}
}
namespace App\Livewire;
use App\Models\Country;
use Livewire\Component;
class ContactForm extends Component
{
public $countries;
public $country_id;
public $city_id;
protected $rules = [
'country_id' => '
{
$this->countries = Country::all();
}
public function save()
{
$validated = $this->validate();
// Use validated data...
}
public function render()
{
return view('livewire.contact-form');
}
}
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public $searchTerm = '';
public $countries = [];
public function updatedSearchTerm($value)
{
$this->countries = Country::where('name', 'like', "%{$value}%")
->limit(50)
->get();
}
// ✅ Correct
$this->country_id = 1;
$this->countries = Country::all(); // Contains id=1
// ❌ Incorrect
$this->country_id = 999; // ID doesn't exist in countries
// If your data uses 'code' instead of 'id'
$countries = [['code' => 'US', 'name' => 'USA']];
// ✅ Correct
public $selected_items = [];
// ❌ Incorrect
public $selected_items; // null, not an array
// Component
public $country_id; // Property name
protected $rules = [
'country_id' => '
public $searchTerm = '';
public $results = [];
public function updatedSearchTerm($value)
{
$this->results = Product::where('name', 'like', "%{$value}%")
->limit(50)
->get();
}
// ❌ Bad - loads all columns
$this->users = User::all();
// ✅ Good - only id and name
$this->users = User::select('id', 'name')->get();
// Livewire Component
public $searchTerm = '';
public $products = [];
public function updatedSearchTerm($value)
{
$this->products = Product::where('name', 'like', "%{$value}%")
->limit(50)
->get();
}
public function mount()
{
$this->countries = Cache::remember('countries', 3600, function () {
return Country::orderBy('name')->get();
});
}
// ❌ Bad - loads all columns
$this->users = User::all();
// ✅ Good - only id and name
$this->users = User::select('id', 'name')->get();
public function updatedSearchTerm($value)
{
$this->users = User::where('name', 'like', "%{$value}%")
->orWhere('email', 'like', "%{$value}%")
->orWhere('phone', 'like', "%{$value}%")
->get();
}
public $selected_items = [1, 3, 5]; // Pre-selected IDs