Download the PHP package spatie/laravel-options without Composer
On this page you can find all versions of the php package spatie/laravel-options. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spatie/laravel-options
More information about spatie/laravel-options
Files in spatie/laravel-options
Package laravel-options
Short Description Create arrays of options from different sources
License MIT
Homepage https://github.com/spatie/laravel-options
Informations about the package laravel-options
Create lists of options from different sources
A typical web application always has many select fields with options. This package makes it simple to transform enums, models, states and arrays to a unified option structure.
Let's say you have the following enum:
You can convert this to options like this:
Which will return the following array:
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
Usage
You can create an Options
object like this (we'll cover other things then enums later on):
You can get an array representation of the options like this:
Or a JSON version like this:
You can return options as part of a response in your controller, and they will be automatically converted into JSON:
Manipulating options
You can sort options by their label like this:
Or use a closure to sort the options:
You can append additional data to the options like this:
This will result in the following options array:
You can filter the options that will be included:
Which will create a smaller options array:
Or reject specific options to be included:
Which will create this options array:
A unique null
option can be added as such:
This will add an option with the value null
:
The label of the null
option can be changed as such:
Another value for null can be set as follows:
It is possible to change the keys used in options by changing the options.php
config file. For example:
Will result in the following options:
With enums
You can create options for native PHP enums, Spatie Enums and MyClabs Enums like this:
By default, the package will look for a static method called labels
with the labels for the enum returned as an array:
Now the options array will look like this:
You can use another method name for the labels as such:
Or use a closure to resolve the label for the enum:
With models
You can create options for Laravel models like this:
Use a single model like this:
Or for a collection of models:
You can also pass a Builder
instance:
By default, the model's key (usually id
) will be taken as a value and the name
field as the label.
You can change the value field like this:
Or use a closure for determining the value:
You can change the label field as such:
Or you can use a closure to resolve the label:
With Select Options
If you're using options for a model in a lot of places, then each time, manually defining the label and/or value fields can become quite tedious:
You can implement Selectable
on a model (or any PHP class), which will automatically convert a model into an option with the correct fields:
Now you can omit the label and value field definitions:
You can also pass some extra data with the SelectOption
like the append
method we saw earlier:
Now the options array will look like this:
As said earlier, implementing Selectable
is not limited to models. You can implement it on any PHP class. In such a case, you can create options like this:
With states
It is possible to create options from the Spatie model states package like this:
You can pass in a model like this (otherwise, a temporary model is created):
The package will automatically look for a label
public method to use as a label:
Or a label
public property:
You can change the name of the method or property which will be used as a label as such:
Or use a closure to resolve the label for the state:
With Arrays
You can create a set of options from an associative array:
You can also use a plain array as such:
In this case, the labels and values will be equal.
Without anything
Lastly, you can create an empty options list like this:
Using for validation
Options, can be converted to a Laravel validation rule:
When options are nullable, the validation rule automatically will become nullable:
Iterating
It is possible to iterator over a set of options:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Ruben Van Assche
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-options with dependencies
illuminate/contracts Version ^8.81|^9.0|^10.0|^11.0
spatie/laravel-package-tools Version ^1.9.2