konekt/enum
Lightweight PHP enum base class (pre-PHP 8.1) for defining value-safe constants via abstract Enum subclasses. Simple instantiation, validation and utilities, with docs and optional Laravel Eloquent integration via konekt/enum-eloquent.
Start by installing the package via Composer: composer require konekt/enum. Then create your first enum by extending \Konekt\Enum\Enum and defining constants — either strings, integers, or null. The most common pattern is string-based enums (e.g., STATUS = 'draft'). Use the magic factory syntax for concise instantiation (e.g., Status::DRAFT()), or new Status('draft') for explicit construction. The __DEFAULT constant lets you define a fallback when no value is passed. Begin with simple domain concepts like OrderStatus, UserRole, or PaymentMethod.
'paid', but work with PaymentStatus::PAID()).konekt/enum-eloquent to automatically cast DB string/int fields to/from enum instances in Laravel models — minimal boilerplate for validation and persistence.function approve(Status $status)) to enforce domain correctness at the method boundary.MyEnum::choices() to generate translated or human-friendly labels for HTML selects in Blade views (@foreach($enum::choices() as $val => $label)).boot() to assign runtime labels (e.g., translations via __()), especially useful for multi-language apps.ValueObject pattern (store raw value internally, convert on access) to migrate gradually away from scalar constants.$labels must be static: Declaring protected $labels (non-static) silently breaks label resolution — always declare it as protected static $labels.__DEFAULT vs create(): Calling Status::create() with no args returns an instance using __DEFAULT, but new Status() also respects it — be consistent in team usage..equals(), has()) and construction are case-sensitive. Mismatched casing (e.g., 'Completed' vs 'completed') fails silently unless validated.__toString() for readable output (e.g., echo $enum shows human-readable label if available, else value). Use value() explicitly when you need the raw value for DB queries or logging.boot() method is perfect for custom initialization logic (e.g., loading labels from config, registering custom logic).const NAME = null; relying on null as a default without defining such a constant will cause errors.How can I help you explore Laravel packages today?