bjerke/laravel-enums
最新稳定版本:v2.1.0
Composer 安装命令:
composer require bjerke/laravel-enums
包简介
Helpers for handling enums and enum translations in Laravel
README 文档
README
This package provides a trait with helper methods for enum management and translation in Laravel.
Important: This package has been rebuilt as of v2 to work with native PHP enums instead of class constants. As such, PHP 8.1 is now required. See UPGRADE.md for more details.
Requirements:
- PHP 8.1 or higher
- Will only work with backed enums
Installation:
composer require bjerke/laravel-enums
Usage:
namespace App\Enums; use Bjerke\Enum\HasTranslations; use Bjerke\Enum\UsesTranslations; enum PostStatus: int implements HasTranslations { use UsesTranslations; case DRAFT = 10; case PUBLISHED = 20; case ARCHIVED = 30; }
Which will then allow you to define the translated versions of these values in Laravel translation file called enums.php:
// ../resources/lang/en/enum.php use App\Enums\MyEnum; return [ 'post_status' => [ PostStatus::DRAFT->value => 'Draft', PostStatus::PUBLISHED->value => 'Published' PostStatus::ARCHIVED->value => 'Archived' ] ];
Which in turn will enable you to fetch the translated values as:
PostStatus::DRAFT->translate(); // return translation for this case PostStatus::getTranslations(); // return all translations
There's also a helper method for retrieving cases:
PostStatus::getCasesAsArray() - Returns an array of "enum key" => "enum value"
统计信息
- 总下载量: 10.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-24