Download the PHP package osama-98/laravel-enum-translatable without Composer
On this page you can find all versions of the php package osama-98/laravel-enum-translatable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download osama-98/laravel-enum-translatable
More information about osama-98/laravel-enum-translatable
Files in osama-98/laravel-enum-translatable
Package laravel-enum-translatable
Short Description A Laravel package that provides translatable enum functionality with easy-to-use methods for working with enum values and their translations
License MIT
Homepage https://github.com/osama-98/laravel-enum-translatable
Informations about the package laravel-enum-translatable
Requirements
- PHP 8.2+
- Laravel 11 or 12
Installation
Quick Start
1. Generate an enum:
2. Add your cases:
3. Add translations in lang/en/enums.php:
4. Use it:
Configuration
Publish the config file to customize supported locales and modular support:
config/laravel-enums.php:
Available Traits
The package ships three traits that build on each other:
| Trait | Best for |
|---|---|
EnumTranslatable |
Enums that need translated labels (includes everything below) |
EnumArrayable |
Enums used for filtering/listing without translation |
EnumWrappable |
Enums that only need comparison and safe-casting helpers |
Generating Enums
Use the make:enum artisan command to scaffold a new enum class:
Note:
--arrayablealready includesEnumWrappableinternally, so there is no need to pass both.
Generated files are placed in app/Enums/. Nested namespaces are supported using /:
The
Enumsuffix is appended automatically if not included in the name.
Usage
All examples use the following enum:
Translation Key Convention
The translation key is derived automatically from the class name:
- Strip the
Enumsuffix - Convert to
snake_case - Pluralize
- Nest under the
enumskey
CourseStatusEnum → enums.course_statuses
Create one enums.php file per locale inside your lang/ directory:
EnumTranslatable
trans(?string $locale = null): string
Returns the translated label for the current case in the given locale (defaults to the app locale). Falls back to the raw value if no translation is found.
allTrans(): array
Returns translations for all locales defined in supported_locales.
toArrayTrans(?string $locale = null): array
Returns all cases as an array of ['value', 'name'] pairs. Uses the current app locale when $locale is null.
toTransCollection(?string $locale = null): Collection
Same as toArrayTrans() but returns a Laravel Collection.
object(): array
Returns the current case as a ['value', 'name'] pair using the current locale. Ideal for API responses.
transKey(): string
Returns the full translation key for the current case.
getTransKey(): string
Returns the translation key for the enum class (without a specific case).
EnumArrayable
names(): array · values(): array · toArray(): array
toCollection(): Collection
Same as toArray() but returns a Laravel Collection, giving you access to all collection methods.
only(array $values): array · except(array $values): array
Filter cases by value. Accepts raw values or enum instances.
randomCase(): self · randomValue(): string
Pick a random case or value, with an optional exclusion list.
matching(string $pattern): array · notMatching(string $pattern): array
Filter cases using a wildcard pattern. Case-insensitive. * matches any characters.
Convenience shortcuts:
EnumWrappable
wrap(BackedEnum|string|null $value, bool $strict = true): ?static
Safely casts a string or an existing enum instance to the enum. Returns null for empty values. Use strict: false to use tryFrom instead of from (no exception on invalid values).
is(BackedEnum|string $value): bool · isNot(BackedEnum|string $value): bool
isAny(array $values): bool · isNotAny(array $values): bool
Real-World Examples
Eloquent Model
Cast a column to an enum automatically:
API Resource
Use object() to return a structured value + label pair:
Select Dropdown (Controller)
Testing
Changelog
Please see CHANGELOG for recent changes.
Credits
- Osama Sadah
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-enum-translatable with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0