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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-options

Create lists of options from different sources

Latest Version on Packagist run-tests PHPStan Total Downloads

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

License

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


All versions of laravel-options with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/contracts Version ^8.81|^9.0|^10.0|^11.0
spatie/laravel-package-tools Version ^1.9.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/laravel-options contains the following files

Loading the files please wait ....