PHP code example of ringlesoft / laravel-selectable
1. Go to this page and download the library: Download ringlesoft/laravel-selectable 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/ */
ringlesoft / laravel-selectable example snippets
$array1 = ["First", "Second", "Third"];
$array2 = ['first' => "First", 'second' => "Second", 'third' => "Third"];
$array3 = [['name' => 'First', 'number' => 1],['name' => 'Second', 'number' => 2],['name' => 'Third', 'number' => 3]];
$options = collect($array)->toSelectOptions();
$options2 = collect($array2)->toSelectOptions();
$options3 = collect($array3)->toSelectable()->withValue('number')->toSelectOptions();
$selectableItems = \App\Models\User::all()->toSelectable()->toSelectItems();
[
[
'label' => 'User Name',
'value' => 'user_id',
'isSelected' => false,
'isDisabled' => false,
'data' => ['hidden' => false],
'classes' => ['form-option', 'custom'],
'id' => null,
],
[...]
]
bladehtml
<select name="user_id">
@foreach($users as $user)
<option value="{{ $user->id }}" {{ $selectedId == $user->id ? 'selected' : '' }}>
{{ $user->name }}
</option>
@endforeach
</select>
bladehtml
<select name="user_id">
{!! $users->toSelectOptions(selected: $selectedId) !!}
</select>
bladehtml
<select name="user_id">
<option value="{{$user->id}}">{{$user->name}}</option>
...
</select>
bladehtml
<select name="user_id">
<option value="{{$user->uuid}}" {{($user->uuid === '6490132934f22') ? 'selected' : '')}}>{{$user->email}}</option>
...
</select>