Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pixelairport's avatar

Get enum by int (reverse)

Hello. I started with enums in PHP/Laravel and did something like that:

enum Status: int
{
    case INITIATED = 0;
    case SUCCESS = 1;
    case DECLINED = 2;
}

public function slug(): string
{
    return match($this) {
        self::INITIATED => 'initiated',
        self::SUCCESS => 'success',
        self::DECLINED => 'declined'
}

In my database I store the id instead of something like 'success' as status to be later flexible. Is it possible to search in my code for the enum? I mean I have now the id for example 2... how can I get 'Status::SUCCESS' from this? Or is that a wrong way to do it?

0 likes
2 replies
Pixelairport's avatar

Thank you @Sofia ... that works and is what I was looking for. Now I can integrate Enum in my workflow. :)

1 like

Please or to participate in this conversation.