Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Enum Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Value Objects & Domain Models: Use enums internally in models as typed value objects, converting to/from underlying values (e.g., store 'paid', but work with PaymentStatus::PAID()).
  • Eloquent Integration: Pair with konekt/enum-eloquent to automatically cast DB string/int fields to/from enum instances in Laravel models — minimal boilerplate for validation and persistence.
  • Validation & Type Hints: Leverage strict type hints (e.g., function approve(Status $status)) to enforce domain correctness at the method boundary.
  • UI Dropdowns: Use MyEnum::choices() to generate translated or human-friendly labels for HTML selects in Blade views (@foreach($enum::choices() as $val => $label)).
  • Booting Dynamic Labels: Override boot() to assign runtime labels (e.g., translations via __()), especially useful for multi-language apps.
  • Backward Compatibility: Use ValueObject pattern (store raw value internally, convert on access) to migrate gradually away from scalar constants.

Gotchas and Tips

  • $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.
  • Case Sensitivity: Enum comparisons (.equals(), has()) and construction are case-sensitive. Mismatched casing (e.g., 'Completed' vs 'completed') fails silently unless validated.
  • No Native Enums Yet: Though v5.0 will target PHP 8.1+ native enums, current versions (v4.x) remain standalone — this enables PHP <8.1 support but adds a layer of indirection.
  • Debugging: Enable __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.
  • Extensibility: The boot() method is perfect for custom initialization logic (e.g., loading labels from config, registering custom logic).
  • Null Handling: Nullable enums require defining a const NAME = null; relying on null as a default without defining such a constant will cause errors.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope