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

dasprid/enum

dasprid/enum brings type-safe, object-based enums to PHP. Define a fixed set of values with labels, comparisons, serialization and validation, reducing magic strings/ints and improving readability in domain models and configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require dasprid/enum. Define an enum by extending DASPRiD\Enum\Enum and declare cases via PHPDoc @method annotations for IDE/static analysis support:

use DASPRiD\Enum\Enum;

/**
 * @method static self DRAFT()
 * @method static self PUBLISHED()
 * @method static self ARCHIVED()
 */
class PostStatus extends Enum {}

Then use it immediately in domain logic: PostStatus::PUBLISHED(), type-hint against the enum, and validate with from().

Implementation Patterns

  • Type-hinting for validation: Inject enums in services (function update(PostStatus $status)) to guarantee only valid values enter business logic.
  • Safe scalar conversion: Use from() for user input ($status = PostStatus::from($request->input('status'))) or database values—throws InvalidArgumentException on mismatch.
  • Discovery & iteration: Iterate PostStatus::values() for dropdowns or API responses, or use self::getKnownValues() for validation.
  • DDD integration: Embed enums in value objects (e.g., OrderStatus::from($dbStatus)) to enforce bounded context boundaries and reduce primitive obsession.
  • Laravel compatibility: Pair with Eloquent via custom casters (e.g., in casts: ['status' => PostStatus::class] if extended), or handle conversion manually—this package alone does not provide native ORM integration.

Gotchas and Tips

  • PHPDoc is mandatory: Omit @method annotations and IDEs/static analyzers (PHPStan, Psalm) won’t recognize enum cases—use @extends Enum<PostStatus> for strict typing.
  • Identity, not equality: Always compare with === (e.g., $status === PostStatus::PUBLISHED()), never ==; from() ensures singleton instances.
  • Serialization pitfalls: Unserialization breaks identity—implement __sleep()/__wakeup() or use JsonSerializable to serialize as scalar values.
  • Performance caveats: Case resolution uses reflection—avoid in tight loops or high-throughput operations; cache lookups if needed.
  • Obsolescence risk: This package is superseded by PHP 8.1+ native enums. If your project targets Laravel 9+ (PHP 8.0+) or PHP 8.1+, do not start with this package—migrate to native enums instead. Verify PHP constraint in composer.json before adoption.
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
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
twbs/bootstrap4