All packages
Enums for Laravel
Front-end options
options() returns a Collection of typed EnumOption DTOs — the list-of-objects shape JS selects expect, with the case name included:
NewsCategory::options();
// Collection<EnumOption{ value: 'hot-news', label: 'Hot News', name: 'HotNews' }, ...>
$option = NewsCategory::options()->first();
$option->value; // 'hot-news'
$option->label; // 'Hot News'
$option->name; // 'HotNews'
$option->toArray();
// ['value' => 'hot-news', 'label' => 'Hot News', 'name' => 'HotNews']The EnumOption DTO
| Member | Type | Value |
|---|---|---|
value | string|int | The backed value — the case name on a pure enum. |
label | string | readable() — translated. |
name | string | The case name. |
toArray() | array | ['value' => ..., 'label' => ..., 'name' => ...] |
RoundlyConsulting\Enums\DataTransferObjects\EnumOption is a final readonly class, so an option can’t be changed once it’s built.
Returning options from a controller
use App\Enums\NewsCategory;
use Illuminate\Http\JsonResponse;
use RoundlyConsulting\Enums\DataTransferObjects\EnumOption;
final class ArticleCategoryController
{
public function __invoke(): JsonResponse
{
return response()->json([
'categories' => NewsCategory::options()
->map(fn (EnumOption $option): array => $option->toArray())
->all(),
]);
}
}
// {"categories":[{"value":"hot-news","label":"Hot News","name":"HotNews"}, ...]}The DTO’s properties are public, so the collection also JSON-encodes to the same shape as-is — pass NewsCategory::options() straight to a JSON response or an Inertia prop.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.