- How do I install the enums-for-laravel package?
- Install it with Composer by running composer require roundly-consulting/enums-for-laravel in the root of your Laravel project. The package is a standard Laravel package, so its service provider is discovered automatically and no manual registration is normally required. Make sure your application runs on PHP 8.1 or newer, since native enums are a PHP 8.1 feature.
- Which PHP and Laravel versions does this package support?
- Because it is built around native PHP enums, the package requires PHP 8.1 or higher. On the Laravel side it targets modern Laravel releases that run on PHP 8.1 and above, which in practice means Laravel 9 and later. Always check the composer.json constraint and the latest release on Packagist before upgrading a major Laravel version.
- Does it work with both backed enums and pure enums?
- Yes. The package is designed for both backed enums, which have scalar values you can persist to the database, and pure enums, which are defined only by their cases. Backed enums are the ones you will typically use for Eloquent attributes and validation, while pure enums work well for internal application logic. The helper methods are intended to behave consistently across both styles.
- How do I cast an Eloquent model attribute to an enum?
- You declare the enum class in your model's casts, and Eloquent will hydrate the attribute into an enum case and persist it back as its scalar value for backed enums. This lets you compare against cases such as Status::Active in your application code instead of raw strings. The package layers convenience helpers on top of this so you can move between case instances, values, and labels without repetitive boilerplate.
- How do I validate an enum value in a form request?
- The package provides a validation rule that you point at an enum class, so incoming request data is checked against the enum's valid cases rather than an inline list of strings. That keeps your validation rules in sync automatically when you add or remove cases. It works in FormRequest classes and inline $request->validate() calls like any other Laravel rule.
- How is this different from Laravel's built-in enum casting?
- Laravel added native enum casting in Laravel 9, and this package is not a replacement for that core feature. What it adds are the conveniences around enums: helper methods for retrieving cases, values, and labels, plus a validation rule and other small utilities. If you only need a basic attribute cast, native Laravel may be enough; if you want less boilerplate across models and forms, this package fills that gap.
- Can I use these enums in database factories and seeders?
- Yes. Because backed enums are persisted by their scalar value, you can assign enum cases directly in factories and seeders and Eloquent will store the correct value. When you need a random case, select one from the enum's cases rather than generating a random string, so you never seed an invalid state. This keeps test data aligned with the same source of truth your application uses.
- How do I get a list of enum cases for a select dropdown?
- The package's helper methods let you pull the cases, values, and human-readable labels from an enum class, which is exactly the shape you need for a Blade select or a JSON payload for a front end. You can map those pairs into an options array in a controller or view composer instead of hard-coding a list of statuses. When a new case is added to the enum, the dropdown updates automatically.
- Do enum attributes serialize correctly in API responses?
- Backed enums serialize to their underlying scalar value when a model is converted to an array or JSON, so API responses stay clean and predictable. If you need a display label rather than the raw value, use the package's helper methods to build the response shape explicitly. Pure enums are better kept out of serialized payloads or mapped manually to a value first.
- Is the package actively maintained, and what should I do if I need a newer Laravel version?
- Check the GitHub repository and Packagist release history for the most recent commits, tags, and the Laravel constraint in composer.json before committing to it in a production project. If your Laravel version is not yet allowed by the constraint, opening an issue or submitting a pull request with the widened constraint is usually the fastest path. Because the package is a thin layer over native enum support, it is also fairly small to fork or vendor if you need a quick patch.