yneet/laravel-collection-select2

A set of useful Laravel collection macros for Select2

1.0 2020-08-24 09:35 UTC

This package is not auto-updated.

Last update: 2024-04-24 12:01:10 UTC


README

This repository contains some useful collection macros for Select2.

Installation

You can pull in the package via composer:

composer require yneet/laravel-collection-select2

The package will automatically register itself.

Macros

toSelect2Grouped

Transform the collection inline to grouped for Select2 Ajax/Data format (see : https://select2.org/data-sources/formats#grouped-data).

$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']
//         ]
//     ],
//     ...
// ];

License

The MIT License (MIT). Please see License File for more information.