a static function ?!
public static function getBy($slug)
{
return match ($slug) {
'ini' => self::INITIATED,
'suc' => self::SUCCESS,
'dec' => self::DECLINED,
};
}
but not sure why you are doing this
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi. I already had this question in the past https://laracasts.com/discuss/channels/site-improvements/get-enum-by-int-reverse?page=1&replyId=773110 , where I asked how can I get the enum for a value. This was the code:
enum Status: int
{
case INITIATED = 0;
case SUCCESS = 1;
case DECLINED = 2;
}
public function slug(): string
{
return match($this) {
self::INITIATED => 'ini',
self::SUCCESS => 'suc',
self::DECLINED => 'dec'
}
and this the soltuion: $status = Status::from(2);
QUESTION:
How can I get the enum by slug? I mean when I have ini for example. How can I get INITIATED enum? Is this possible?
a static function ?!
public static function getBy($slug)
{
return match ($slug) {
'ini' => self::INITIATED,
'suc' => self::SUCCESS,
'dec' => self::DECLINED,
};
}
but not sure why you are doing this
Please or to participate in this conversation.