nasyrov / laravel-enums

Laravel package for Enum implementation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get Enum value from it's ID

ablishek opened this issue · comments

Hi,

this may be a silly question, but I wasn't able to find out how to extract the enum name based on the type.

Is there anyway I can do the following:
App\Enum\GenericEnum::where('value', 1);
and this would return the label "1stEnum"

In few words, search for the value and get the label without having to loop.

As a work around, I did the following:

option 1

$value = 1;
\App\Enums\GenericEnum::labels()[$value];

option2
throw array into a Laravel 'collect' allowing you to search, filter, and use properties as any other eloquent model.

Hi @abhi-cognitivo,

Sorry for the late reply!
If you have a look at the source code I got 'constants()' method that returns the laravel collection.

$collection = \App\Enums\GenericEnum::constants();

Give it a try!

Cheers,
Evgenii

Great thanks!