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

Laravel Enum Laravel Package

spatie/laravel-enum

Laravel integration for spatie/enum: use Enum base class in Laravel, cast model attributes to enums (including nullable and arrays), and get Laravel-friendly behavior via custom casts and Castable support for Eloquent.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Feature Development: Enables type-safe, self-documenting enums for Laravel models, APIs, and business logic (e.g., OrderStatus::PENDING, UserRole::ADMIN). Reduces magic strings and improves IDE support (autocompletion, refactoring).
  • Roadmap Alignment: Critical for projects adopting Laravel 10+ or migrating legacy systems to structured data validation. Supports future-proofing with PHP 8.1+ enums while maintaining backward compatibility.
  • Build vs. Buy: Avoids reinventing enum patterns (e.g., custom traits or classes) and leverages Spatie’s battle-tested, community-vetted solution. Low maintenance cost with MIT license.
  • Use Cases:
    • Domain Modeling: Define finite states (e.g., PaymentStatus, SubscriptionTier) with validation.
    • API Contracts: Standardize response payloads (e.g., HTTPStatus::SUCCESS) and request validation.
    • Database Integrity: Enforce enum constraints in migrations (e.g., enum('active', 'suspended')).
    • Localization: Easily extend enums with translated labels (e.g., OrderStatus::CANCELLED->label()).

When to Consider This Package

  • Adopt When:
    • Your Laravel app has repeated string constants (e.g., 'active', 'pending') across models/controllers.
    • You need runtime validation (e.g., reject invalid UserRole values early).
    • Your team uses PHP 8.1+ enums but wants Laravel-specific features (e.g., Eloquent casting, API responses).
    • You prioritize developer experience (autocompletion, type safety) over minimalism.
  • Look Elsewhere If:
    • You’re using non-Laravel PHP (this is Laravel-specific; use spatie/enum directly).
    • Your enums are trivial (e.g., 2–3 values) and don’t need validation or database constraints.
    • You require complex nested enums (this package focuses on simplicity; consider custom solutions).
    • Your team prefers native PHP 8.1 enums without Laravel integration (though this package bridges both).

How to Pitch It (Stakeholders)

For Executives: "This package lets us replace error-prone string constants with type-safe enums—like OrderStatus::SHIPPED—across our Laravel app. It cuts bugs in validation logic, speeds up development with IDE autocompletion, and future-proofs our codebase for PHP 8.1+. Spatie’s MIT-licensed solution is used by 300+ teams, with minimal maintenance overhead. For a one-time composer require, we gain consistency, scalability, and developer happiness—critical for our [roadmap item X]."

For Engineers: *"Spatie’s laravel-enum gives us:

  • Eloquent casting: Automatically cast database strings to enums (e.g., User::find(1)->role returns UserRole::ADMIN).
  • API-friendly: Serialize enums to strings/JSON with ->value or labels with ->label().
  • Validation: Reject invalid values early (e.g., User::create(['role' => 'hacker']) fails fast).
  • Zero boilerplate: Works with native PHP 8.1 enums or Spatie’s lightweight enums.
  • Database safety: Generate migrations with enum() constraints. Example: Replace if ($status === 'pending') with $status->isPending()—self-documenting and refactor-safe. Let’s prototype this for [feature Y] and measure dev velocity gains."*
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