Create Select Options from Enums, Laravel Models, and more

Published on : July 27,2022
Create Select Options from Enums, Laravel Models, and more

Laravel Options by Spatie is a package to 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.

An example from the readme illustrates how this package can help ease working with select options in Laravel applications. Given an enum, you can use this package to build options:

enum Hobbit: string
{
    case Frodo = 'frodo';
    case Sam = 'sam';
    case Merry = 'merry';
    case Pippin = 'pippin';
}
 
Options::forEnum(Hobbit::class)->toArray();
 
/* returns the following array
[
    ['label' => 'Frodo', 'value' => 'frodo'],
    ['label' => 'Sam', 'value' => 'sam'],
    ['label' => 'Merry', 'value' => 'merry'],
    ['label' => 'Pippin', 'value' => 'pippin'],
]
*/
 
// Or return JSON
Options::forEnum(Hobbit::class)->toJson();

Not only can you use Enums, but this package also supports data from various sources, including:

  • Models
  • Model States (via Spatie Model States)
  • Arrays
  • Manually

Another neat feature is that you can turn your options into a validation rule:

$request->validate([
    // ['in:frodo,sam,merry,pippin']
    'hobbit' => Options::forEnum(Hobbit::class)->toValidationRule()
]);

 

 

Categories : Laravel PHP

Tags : Laravel Packages Laravel 9

Praful Sangani
Praful Sangani
I'm a passionate full-stack developer with expertise in PHP, Laravel, Angular, React Js, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap. I enjoy sharing my knowledge by writing tutorials and providing tips to others in the industry. I prioritize consistency and hard work, and I always aim to improve my skills to keep up with the latest advancements. As the owner of Open Code Solution, I'm committed to providing high-quality services to help clients achieve their business goals.


25 Comments

fenofibrate 160mg sale oral fenofibrate order tricor 200mg for sale


ketotifen 1mg usa buy cheap geodon tofranil over the counter


cialis 40mg for sale sildenafil pharmacy sildenafil 100mg brand


acarbose over the counter order repaglinide online fulvicin 250mg pill


aspirin 75 mg us zovirax tubes zovirax brand


purchase meloset for sale order melatonin 3mg pills danocrine 100mg ca


buy florinef generic pill florinef buy imodium 2 mg generic


prasugrel online buy detrol for sale order tolterodine 2mg for sale


monograph 600 mg us purchase monograph generic buy generic cilostazol


pyridostigmine sale order maxalt 5mg without prescription purchase rizatriptan for sale


purchase enalapril generic order duphalac for sale lactulose for sale online


order betahistine 16 mg betahistine 16mg canada purchase probenecid generic


purchase latanoprost online cheap generic xalatan purchase rivastigmine generic


omeprazole 10mg canada omeprazole order online buy cheap generic metoprolol


premarin 600 mg price buy generic premarin online sildenafil fast shipping


micardis 20mg generic buy telmisartan 20mg pills movfor order


cenforce 50mg usa order chloroquine 250mg without prescription buy cheap generic aralen


cialis 20mg pill buy viagra pills buy viagra 100mg online cheap


modafinil 200mg price promethazine medication prednisone 5mg canada


buy cefdinir 300 mg generic order lansoprazole 30mg generic order lansoprazole generic


cheap isotretinoin buy accutane zithromax order


buy lipitor generic order albuterol 100mcg online cheap buy generic amlodipine over the counter


order azithromycin 500mg without prescription order azithromycin pills order neurontin 600mg pill


blackjack vegas free online games gambling casino furosemide 40mg tablet


order protonix 20mg pill phenazopyridine 200mg tablet where can i buy phenazopyridine


Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles

Access specifier in php
Praful Sangani By Praful Sangani - July 20,2022