PHP code example of mrjokermr / livewire-multi-select
1. Go to this page and download the library: Download mrjokermr/livewire-multi-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/ */
mrjokermr / livewire-multi-select example snippets
// In your Livewire Component
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
public array $userIds = [];
public function getSettings()
{
return SelectSettings::simple(
source: [1 => 'Example user', 2 => 'Second user'],
)->setLabel('Users')->setCloseOnSelect(false);
}
// In your Blade view
<livewire:multi-select
wire:model.live="userIds"
:settings="$this->getSettings()"
/>
// In your Livewire Component
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
use App\Models\User;
public array $userIds = [];
public function getSettings()
{
return SelectSettings::eloquentModel(
class: User::class,
keyAttribute: 'id',
labelAttribute: 'name',
limit: 50, // Optional
)->setLabel('Users');
}
// In your Blade view
<livewire:multi-select
wire:model.live="users"
:settings="$this->getSettings()"
/>
// In your Livewire Component
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
use App\Models\User;
public function getSettings()
{
return SelectSettings::eloquentModel(
class: User::class,
keyAttribute: 'id',
labelAttribute: 'name',
)->singleValueMode();
}
// In your Blade view
<livewire:multi-select
wire:model.live="user"
:settings="$this->getSettings()"
/>
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
// For Eloquent models
SelectSettings::eloquentModel(
class: \App\Models\User::class,
keyAttribute: 'id',
labelAttribute: 'name',
)->enableSearch();
// For simple arrays
SelectSettings::simple(
source: [1 => 'Example user', 2 => 'Second user'],
)->enableSearch();
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
SelectSettings::eloquentModel(
class: \App\Models\User::class,
keyAttribute: 'id',
labelAttribute: 'name',
)->enableSearch(attributes: ['email']); // Searches 'name' and 'email'
->setCssClasses(
inputSearch: 'input-search',
)
// In your Livewire Component
use Livewire\Attributes\On;
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
use App\Models\Chapter;
public function getSettings()
{
return SelectSettings::simple(
source: Chapter::all()->pluck('name', 'id')->toArray(),
event: 'chapters_updated',
)->setLabel('Chapters');
}
// Listen for the event
#[On('chapters_updated')]
public function handleChapterSelection(array $selection)
{
// $selection contains the array of selected chapter IDs
}
use Mrjokermr\LivewireMultiSelect\Classes\SelectSettings;
use App\Models\User;
public function getSettings()
{
return SelectSettings::eloquentModel(
class: User::class,
keyAttribute: 'id',
labelAttribute: 'name',
baseQuery: User::where('email', 'LIKE', '%example.com%')
)->setLabel('Users');
}
// In your Blade view
<livewire:multi-select
wire:model.live="users"
:settings="$this->getSettings()"
/>