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

daverandom/enum

daverandom/enum adds a lightweight enum implementation for PHP, providing type-safe sets of named values with strict validation and easy comparison. Useful for replacing magic numbers/strings in domain models, configs, and APIs.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides a simple base class for defining enumerations in PHP. To get started:

  1. Require it via Composer: composer require daverandom/enum
  2. Extend the DaveRandom\Enum class for your enum types
  3. Define valid enum values as class constants
  4. Instantiate enums using MyEnum::valueOf($value) or MyEnum::fromString($value)

First use case: Model status constants in a clean, type-safe way instead of raw strings or ints.

use DaveRandom\Enum;

class OrderStatus extends Enum
{
    const PENDING = 'pending';
    const SHIPPED = 'shipped';
    const DELIVERED = 'delivered';
}

$status = OrderStatus::valueOf('pending'); // returns instance of OrderStatus

Implementation Patterns

  • Use enum classes for domain concepts with fixed sets of values (e.g., PaymentMethod, UserRole, MediaType)
  • Leverage ::isValid($value) and ::getAll() for validation and dropdown population
  • Override __construct() only for custom logic (rarely needed)
  • Type-hint against the enum class in services or controllers for better IDE autocomplete and static analysis
  • Combine with DTOs (Data Transfer Objects) to enforce enum constraints in request payloads
  • Use === for comparison (enum instances are immutable and singleton per value)

Gotchas and Tips

  • The package is unmaintained (last release 2018); consider modern alternatives like myclabs/php-enum or native-backed enums in PHP 8.1+
  • valueOf() throws an exception for invalid values — wrap calls with try/catch or use isValid() first
  • Ensure case-sensitive matches — constants like PENDING = 'Pending' will not match input 'pending'
  • Enums are immutable; methods returning self will return new instances if reassignment occurs (though this class uses string-based singleton caching internally)
  • PHPDoc type hints (@return OrderStatus) help IDEs but don’t enforce type safety at runtime beyond comparisons
  • If serializing to JSON, override jsonSerialize() to return the underlying scalar value ($this->getValue())
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport