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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-enum-translatable

![Laravel Enum Translatable](.github/logo.svg) [![Latest Version on Packagist](https://img.shields.io/packagist/v/osama-98/laravel-enum-translatable.svg?style=flat-square)](https://packagist.org/packages/osama-98/laravel-enum-translatable) [![Total Downloads](https://img.shields.io/packagist/dt/osama-98/laravel-enum-translatable.svg)](https://packagist.org/packages/osama-98/laravel-enum-translatable) [![License](https://img.shields.io/packagist/l/osama-98/laravel-enum-translatable.svg?style=flat-square)](https://packagist.org/packages/osama-98/laravel-enum-translatable) **Supercharge your Laravel enums with translations, array helpers, and comparison methods — all via simple traits.** [Medium Article](https://masteryoflaravel.medium.com/stop-hardcoding-translations-the-revolutionary-way-to-build-multilingual-laravel-apps-with-bf303533b8b0) · [Laravel News](https://laravel-news.com/translatable-enums)

Requirements


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: --arrayable already includes EnumWrappable internally, so there is no need to pass both.

Generated files are placed in app/Enums/. Nested namespaces are supported using /:

The Enum suffix 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:

CourseStatusEnumenums.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

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-enum-translatable with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package osama-98/laravel-enum-translatable contains the following files

Loading the files please wait ...