PHP code example of ajjitech / searchable-select

1. Go to this page and download the library: Download ajjitech/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/ */

    

ajjitech / 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()
    {
        $this->countries = Country::orderBy('name')->get();
    }

    public function save()
    {
        $this->validate([
            'country_id' => '

class UserProfile extends Component
{
    public $countries;
    public $country_id;

    public function mount()
    {
        $this->countries = Country::orderBy('name')->get();
    }
}

class UserSkills extends Component
{
    public $skills;
    public array $selected_skills = [];

    public function mount()
    {
        $this->skills = Skill::orderBy('name')->get();
    }
}

class LocationSelector extends Component
{
    public $countries;
    public $regions = [];
    public $cities  = [];

    public $country_id;
    public $region_id;
    public $city_id;

    public function mount()
    {
        $this->countries = Country::orderBy('name')->get();
    }

    public function updatedCountryId($value)
    {
        $this->regions   = Region::where('country_id', $value)->orderBy('name')->get();
        $this->region_id = null;
        $this->city_id   = null;
        $this->cities    = [];
    }

    public function updatedRegionId($value)
    {
        $this->cities  = City::where('region_id', $value)->orderBy('name')->get();
        $this->city_id = null;
    }
}

public $locations = [
    [
        'label'   => 'North America',
        'options' => [
            ['id' => 1, 'name' => 'United States'],
            ['id' => 2, 'name' => 'Canada'],
            ['id' => 3, 'name' => 'Mexico'],
        ],
    ],
    [
        'label'   => 'Europe',
        'options' => [
            ['id' => 4, 'name' => 'United Kingdom'],
            ['id' => 5, 'name' => 'France'],
            ['id' => 6, 'name' => 'Germany'],
        ],
    ],
];

public $categories = [
    [
        'category_name' => 'Fruits',
        'items' => [
            ['code' => 'APL', 'title' => 'Apple'],
            ['code' => 'BAN', 'title' => 'Banana'],
        ],
    ],
];

public $products = [
    ['sku' => 'PROD-001', 'product_name' => 'Laptop'],
    ['sku' => 'PROD-002', 'product_name' => 'Mouse'],
];

public $selected_sku;

public function save()
{
    $this->validate([
        'country_id' => '

public function updated($propertyName)
{
    $this->validateOnly($propertyName);
}

public $statuses = [
    ['id' => 'draft',     'name' => 'Draft'],
    ['id' => 'published', 'name' => 'Published'],
    ['id' => 'archived',  'name' => 'Archived'],
];

public $searchTerm = '';
public $countries  = [];

public function updatedSearchTerm($value)
{
    $this->countries = Country::where('name', 'like', "%{$value}%")
        ->limit(50)
        ->get();
}

// Correct
public array $selected_items = [];

// Wrong — null, not an array
public $selected_items;

// If your data uses 'code' instead of 'id'
$countries = [['code' => 'US', 'name' => 'USA']];

public $searchTerm = '';
public $products   = [];

public function updatedSearchTerm($value)
{
    $this->products = Product::where('name', 'like', "%{$value}%")
        ->limit(50)
        ->get();
}

// Good — only id and name
$this->users = User::select('id', 'name')->get();

public function mount()
{
    $this->countries = Cache::remember('countries', 3600, fn () =>
        Country::orderBy('name')->get()
    );
}
bash
php artisan vendor:publish --tag=searchable-select-views
bash
php artisan make:livewire ContactForm
bash
php artisan vendor:publish --tag=searchable-select-views
bash
   php artisan view:clear
   
bash
cd demo
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan serve