PHPackages                             ringlesoft/laravel-selectable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ringlesoft/laravel-selectable

ActiveLaravel-package[Utility &amp; Helpers](/categories/utility)

ringlesoft/laravel-selectable
=============================

A package to populate select options from laravel collections.

1.0.5(1y ago)5222MITPHPPHP &gt;=8.1

Since May 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ringlesoft/laravel-selectable)[ Packagist](https://packagist.org/packages/ringlesoft/laravel-selectable)[ Docs](https://ringlesoft.com/packages/laravel-selectable)[ RSS](/packages/ringlesoft-laravel-selectable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (8)Used By (0)

Laravel Selectable
==================

[](#laravel-selectable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5361a889a633b8ab24139bf0cdf3f44dc2bba33523bd8b1cf54e80e92155745e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72696e676c65736f66742f6c61726176656c2d73656c65637461626c652e737667)](https://packagist.org/packages/ringlesoft/laravel-selectable)[![Total Downloads](https://camo.githubusercontent.com/8dea0cff45367ccbaa7d16c4b9f5f8ac6dc423be309e8f2ab291588284c2f8f5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72696e676c65736f66742f6c61726176656c2d73656c65637461626c652e737667)](https://packagist.org/packages/ringlesoft/laravel-selectable)[![PHP Version Require](https://camo.githubusercontent.com/4c454d0c5fb4b5d763f763826213ab40c901fbff4338efb62f56301fdcb2a92f/68747470733a2f2f706f7365722e707567782e6f72672f72696e676c65736f66742f6c61726176656c2d73656c65637461626c652f726571756972652f706870)](https://packagist.org/ringlesoft/laravel-selectable)[![Dependents](https://camo.githubusercontent.com/5fbc9f8cc7b52ccef70f83176ef083f68c4df31e412703f9c4eea9e2a9d47275/68747470733a2f2f706f7365722e707567782e6f72672f72696e676c65736f66742f6c61726176656c2d73656c65637461626c652f646570656e64656e7473)](https://packagist.org/packages/ringlesoft/laravel-selectable)

---

Laravel Selectable is a powerful package that simplifies the process of generating HTML select options from Laravel collections. With its flexible and intuitive syntax, you can easily create customized select options without the need for additional traits or class extensions.

Features
--------

[](#features)

- Generate select options from Laravel collections with ease
- Customize label and value fields using strings or closures
- Specify selected and disabled options
- Add data attributes and classes to options
- Support for grouping options
- Convert collections to an array of selectable items for AJAX responses or SPAs
- Utilize the power Laravel's collections for more advanced filtering and sorting

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require ringlesoft/laravel-selectable
```

 No additional configuration is required.

Usage
-----

[](#usage)

### 1. Basic Usage

[](#1-basic-usage)

```

    {!! \\App\\Models\\User::all()->toSelectOptions(); !!}}

```

This will generate a select dropdown with options for all users, using the name field as the label and the id field as the value.

```

    {{$user->name}}
    ...

```

### 2. Inline Customization

[](#2-inline-customization)

```

    {!! \\App\\Models\\User::all()->toSelectOptions('email', 'uuid', '6490132934f22'); !!}}

```

This will generate a select dropdown with options for all users, using the `email` field as the label and the `uuid`field as the value. The selected option will be the user with the `uuid` 6490132934f22.

```

    uuid === '6490132934f22') ? 'selected' : '')}}>{{$user->email}}
    ...

```

#### Method parameters

[](#method-parameters)

- `label`: The name of the field to be used as the label for the option. If a closure is provided, the result of the closure will be used as the label. Default is `name`.
- `value`: The name of the field to be used as the value for the option. If a closure is provided, the result of the closure will be used as the value. Default is `id`.
- `selected`: The value of the item to be used as the selected option. Can be value, array of values or closure.
- `disabled`: The value of the item to be used as the disabled option. Can be value, array of values or closure.

### 3. Advanced Usage

[](#3-advanced-usage)

This package allows building of select options from a `Selectable` object using method chaining. The method `toSelectable()` is used to convert the collection into a `Selectable` object. The `Selectable` object has several methods that allow you to customize the options and their properties. The `toSelectOptions` method is used to convert the `Selectable` object into html select options.

```

    {!!
    \App\Models\User::all()
    ->toSelectable()
    ->withValue('id')
    ->withLabel(fn($user) => "{$user->first_name} {$user->last_name}")
    ->withSelected([2, 3])
    ->withDisabled(fn($item) => $item->status = 'inactive')
    ->withDataAttribute('hidden', fn($item) => $item->status !== 'active')
    ->withClass('form-option custom')
    ->toSelectOptions();
    !!}

```

This will generate a multi-select dropdown with options for all users, using the `id` field as the `value`, and a combination of the `first_name` and `last_name` fields as the `label`. Options with IDs `2` and `3` will be selected by default, and options with an '`inactive`' `status` will be disabled. A '`data-hidden`' attribute will be added to options with a `status`other than '`active`', and a custom class '`form-option custom`' will be applied to all options.

```

    David Moore
    John Doe
    Jane Doe
    Mark Manson

```

#### Available methods

[](#available-methods)

- `withLabel(string|Closure $label)`: This method allows you to customize the label for each option. A string will be used as the collection field from which the label will be generated, while a Closure will be used to generate the label.
- `withValue(string|Closure $value)`: This method allows you to customize the value for each option. A string will be used as the collection field from which the value will be generated, while a Closure will be used to generate the value.
- `withSelected(mixed|Closure $selected)`: This method allows you to customize the selected options. Can be a `string`, `int`, an array of `string`/`int`, a `model` or a `Closure` that returns a `boolean` value.
- `withDisabled(mixed|Closure $disabled)`: This method allows you to customize the disabled options. Can be a `string`, `int`, an array of `string`/`int`, a `model` or a `Closure` that returns a `boolean` value.
- `withDataAttribute(string|Closure $attribute, mixed|Closure $value)`: This method allows you to add a data attribute to each option. The first parameter can be a `string` or a `Closure` that returns a `string` which will be attached as `data-{attribute}="{value}"` to the option. The second parameter can be any type convertable to a string or a `Closure` that returns a `string`.
- `withId(string|Closure $id)`: This method allows you to add an `id` attribute to each option. The value can be a `Closure` that returns a unique `string` for each option.
- `withClass(string|array|Closure $class)`: This method allows you to add a class to each option. The value can be a `string` or an `array` of `string` or a `Closure` that returns a `string`.
- `toSelectItems()`: This method converts the selectable collection to an `array` of selectable items. Useful for Ajax responses or SPAs.
- `toSelectOptions()`: This method converts the selectable collection to an HTML select options string.
- Some of the methods from `Illuminate\Support\Collection` are also available including `groupBy()`.

> **Note:** Writing queries within blade templates is not recommended. This is only for simplifying demonstration

### Non-object Array Collections

[](#non-object-array-collections)

You can work with collections of non-object arrays both flat and associative

```
    $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();
```

Getting Selectable Items
------------------------

[](#getting-selectable-items)

```
    $selectableItems = \App\Models\User::all()->toSelectable()->toSelectItems();
```

This will convert the collection of users into an array of selectable items, which can be useful for AJAX responses or Single Page Applications (SPAs).

#### Structure of Selectable Items

[](#structure-of-selectable-items)

```
    [
        [
            'label' => 'User Name',
            'value' => 'user_id',
            'selected' => false,
            'disabled' => false,
            'dataAttributes' => ['hidden' => false],
            'classes' => ['form-option', 'custom'],
        ],
        [...]
    ]
```

Testing
-------

[](#testing)

To run tests, run the following command:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [David Ringle](https://github.com/ringunger) (Author)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance48

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~56 days

Recently: every ~73 days

Total

7

Last Release

383d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e6e20f0cc6e0f58ef2b8460f08d3d4dfe10d69aaaf352a1b5a201f13eb236d4?d=identicon)[ringunger](/maintainers/ringunger)

---

Top Contributors

[![ringunger](https://avatars.githubusercontent.com/u/6137527?v=4)](https://github.com/ringunger "ringunger (34 commits)")

---

Tags

collectionlaraveloptionsselectablelaraveloptionscollectionringlesoftselectableselect-options

### Embed Badge

![Health badge](/badges/ringlesoft-laravel-selectable/health.svg)

```
[![Health](https://phpackages.com/badges/ringlesoft-laravel-selectable/health.svg)](https://phpackages.com/packages/ringlesoft-laravel-selectable)
```

###  Alternatives

[cerbero/lazy-json

Framework-agnostic package to load JSONs of any dimension and from any source into Laravel lazy collections.

254309.8k1](/packages/cerbero-lazy-json)[glhd/conveyor-belt

14797.0k](/packages/glhd-conveyor-belt)[gamez/typed-collection

Type-safe collections based on Laravel Collections

45317.8k](/packages/gamez-typed-collection)[lazerg/laravel-enum-pro

A powerful PHP enum extension with collection support, random selection, and magic static calls

4319.0k](/packages/lazerg-laravel-enum-pro)[werxe/laravel-collection-macros

Custom Laravel Collection macros.

2625.8k](/packages/werxe-laravel-collection-macros)[prologue/support

Prologue Support is an extension for Illuminate Support

1616.8k](/packages/prologue-support)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
