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',
            'selected' => false,
            'disabled' => false,
            'dataAttributes' => ['hidden' => false],
            'classes' => ['form-option', 'custom'],
        ],
        [...]
    ]
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>