PHP code example of fyyyn1210 / wire-select

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

    

fyyyn1210 / wire-select example snippets


// database/migrations/create_users_table.php
Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email');
    $table->timestamps();
});

// database/seeders/UserSeeder.php
User::factory()->count(50)->create();

// app/Livewire/UserForm.php


namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\On;

class UserForm extends Component
{
    public $user_id;
    public $formData = [];

    #[On('selectionChanged')]
    public function handleUserSelection($data)
    {
        if ($data['field'] === 'user_id') {
            $this->user_id = $data['value'];
        }
    }

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

// app/Livewire/CategoryForm.php


namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\On;

class CategoryForm extends Component
{
    public $categories = [
        1 => 'Technology',
        2 => 'Business',
        3 => 'Science',
        4 => 'Health',
        5 => 'Education'
    ];

    public $selected_category;

    #[On('selectionChanged')]
    public function handleSelection($data)
    {
        if ($data['field'] === 'category') {
            $this->selected_category = $data['value'];
        }
    }

    public function render()
    {
        return view('livewire.category-form');
    }
}

// app/Livewire/ProductForm.php


namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\On;

class ProductForm extends Component
{
    public $product_id;
    public $category_id = 1; // Pre-selected category

    #[On('selectionChanged')]
    public function handleProductSelection($data)
    {
        if ($data['field'] === 'product_id') {
            $this->product_id = $data['value'];
        }
    }

    public function render()
    {
        return view('livewire.product-form');
    }
}



return [
    'defaults' => [
        'placeholder' => 'Search...',
        'empty_message' => 'No data found',
        'max_height' => '250px',
        'limit' => 10,
        'icon' => 'ki-search',
        'icon_path' => 2,
        '

// Old API Route
Route::get('/users/search', function (Request $request) {
    $users = User::where('name', 'like', '%' . $request->q . '%')
                 ->orWhere('email', 'like', '%' . $request->q . '%')
                 ->paginate(30);

    return response()->json([
        'items' => $users->items(),
        'total_count' => $users->total()
    ]);
});

// Handle selection in your Livewire component
#[On('selectionChanged')]
public function handleUserSelection($data)
{
    if ($data['field'] === 'user_id') {
        $this->user_id = $data['value'];
    }
}

// Make sure your database table exists and has the specified columns
// Check your .env database configuration
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

// Make sure you're using the correct event listener
use Livewire\Attributes\On;

#[On('selectionChanged')]
public function handleSelection($data)
{
    // Your logic here
}
bash
# Publish config file for customization
php artisan vendor:publish --provider="Fyyyn1210\WireSelect\WireSelectServiceProvider" --tag="config"

# This creates: config/wire-select.php
bash
# Publish CSS/JS assets
php artisan vendor:publish --provider="Fyyyn1210\WireSelect\WireSelectServiceProvider" --tag="assets"

# This creates: public/vendor/wire-select/
blade
<!-- resources/views/livewire/category-form.blade.php -->
<div>
    <livewire:wire-select-box
        :items="$categories"
        field-name="category"
        label="Select Category"
        placeholder="Search categories..."
        icon="ki-category"
        :selected-value="$selected_category" />
</div>
bash
# Error: Component [wire-select-box] not found
# Solution: Clear cache
php artisan config:clear
php artisan view:clear
composer dump-autoload