PHP code example of yneet / laravel-collection-select2

1. Go to this page and download the library: Download yneet/laravel-collection-select2 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/ */

    

yneet / laravel-collection-select2 example snippets


$collection = collect([
    ['text' => 'Title 1', 'id' => 1, 'group' => 'Group 1'],
    ['text' => 'Title 2', 'id' => 2, 'group' => 'Group 1'],
    ...
]);

$collection->toSelect2Grouped('group');

// [
//     [
//         'text' => 'Group 1',
//         'children' => [
//             ['text' => 'Title 1', 'id' => 1, 'group' => 'Group 1'],
//             ['text' => 'Title 2', 'id' => 2, 'group' => 'Group 1']
//         ]
//     ],
//     ...
// ];