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/ */
// 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');
}
}
// 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