Names, values and labels
Get every case’s name, backed value or readable label as an Illuminate\Support\Collection, in declaration order — call ->all() for a plain array:
NewsCategory::names()->all(); // ['HotNews', 'RegularNews', 'PrivateNews']
NewsCategory::values()->all(); // ['hot-news', 'regular-news', 'private-news']
NewsCategory::storable()->all(); // ['hot-news', 'regular-news', 'private-news']
NewsCategory::labels()->all(); // ['Hot News', 'Regular News', 'Private News']
NewsCategory::collect(); // Collection<NewsCategory> — every case as an instance
NewsCategory::count(); // 3| Method | Returns | Contents |
|---|---|---|
names() | Collection<int, string> | Case names. |
values() | Collection<int, string|int> | Backed values (case names on a pure enum). |
storable() | Collection<int, string|int> | Same as values(). |
labels() | Collection<int, string> | readable() of every case. |
collect() | Collection<int, self> | Every case as an enum instance. |
count() | int | The number of cases. |
Persisting the allowed set
storable() is the persistence-flavoured alias of values() — both return the raw backed values (the case names on a pure enum), handy for validation rules, database enum columns or persisting an allowed set:
use App\Enums\NewsCategory;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::create('articles', function (Blueprint $table): void {
$table->id();
$table->string('title');
$table->enum('category', NewsCategory::storable()->all());
$table->timestamps();
});Chaining
Everything returns a regular Illuminate\Support\Collection, so the full Collection API is available:
NewsCategory::values()->all(); // plain PHP array
NewsCategory::labels()->implode(', '); // "Hot News, Regular News, Private News"
NewsCategory::collect()
->reject(fn (NewsCategory $category): bool => $category->is(NewsCategory::PrivateNews))
->map(fn (NewsCategory $category): string => $category->label())
->values()
->all(); // ['Hot News', 'Regular News']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.