Select options
toOptions() returns a value => label Collection, ready for a <select> element or a form component; toArray() is the same map as a plain array for JSON and API responses:
NewsCategory::toOptions();
// Collection: ['hot-news' => 'Hot News', 'regular-news' => 'Regular News', 'private-news' => 'Private News']
NewsCategory::toArray();
// ['hot-news' => 'Hot News', 'regular-news' => 'Regular News', 'private-news' => 'Private News']Not in config files
Labels are translated, so build options at runtime — in controllers, views and resources — not inside a config/*.php file: config loads before the translator exists, and a cached config would freeze the labels in one locale. A config file can hold the translation-free values() instead:
use App\Enums\NewsCategory;
// config/news.php — translation-free, safe to cache
return [
'categories' => NewsCategory::values()->all(),
];In a Blade form
Pass the options to the view and render them — the keys are exactly the backed values your validation rule accepts:
use App\Enums\NewsCategory;
use Illuminate\Contracts\View\View;
final class CreateArticleController
{
public function __invoke(): View
{
return view('articles.create', [
'categories' => NewsCategory::toOptions(),
]);
}
}<select name="category">
@foreach ($categories as $value => $label)
<option value="{{ $value }}" @selected(old('category') === $value)>{{ $label }}</option>
@endforeach
</select>Labels follow the active locale, so a translated app renders translated options with no extra code. On a pure enum the map is keyed by case name; on an int-backed enum the keys are ints.
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.