mahmudul/lara-enum
Composer 安装命令:
composer require mahmudul/lara-enum
包简介
A lightweight Laravel enum enhancement package with labels, descriptions and helpers.
README 文档
README
Laravel custom enum utilities that add helpful attributes and helpers to your native PHP 8.1+ enums. Provides:
- A Description attribute you can place on enum cases for human-friendly labels
- A Translatable attribute to localize labels via a translation key (uses Laravel's __() helper)
- A HasEnumAttributes trait that gives you convenient helpers like label(), values(), and asOptions()
Requirements
- PHP: ^8.4
- Illuminate Support: ^12.0 or ^13.0 (works in Laravel applications using these versions)
Installation
Install via Composer:
composer require mahmudul/lara-enum
The package uses Laravel package auto-discovery. No manual provider registration is needed.
Usage
1) Define your enum
Add the Description or Translatable attribute to cases and use the HasEnumAttributes trait to gain helpers.
<?php namespace App\Enums; use Mahmudul\LaraEnum\Attributes\Description; use Mahmudul\LaraEnum\Traits\HasEnumAttributes; enum Status: string { use HasEnumAttributes; #[Description('Draft')] case DRAFT = 'draft'; #[Description('Published')] case PUBLISHED = 'published'; // If no Description is provided, label() falls back to a humanized case name case IN_REVIEW = 'in_review'; }
Using Translatable (for localized labels)
<?php namespace App\Enums; use Mahmudul\LaraEnum\Attributes\Translatable; use Mahmudul\LaraEnum\Traits\HasEnumAttributes; enum PaymentStatus: string { use HasEnumAttributes; #[Translatable('enums.payment_status.pending')] case PENDING = 'pending'; #[Translatable('enums.payment_status.paid')] case PAID = 'paid'; } // resources/lang/en/enums.php return [ 'payment_status' => [ 'pending' => 'Pending', 'paid' => 'Paid', ], ]; // Usage (will call Laravel's __() helper under the hood): PaymentStatus::PAID->label(); // "Paid"
Note: Translatable uses Laravel's __() helper, so ensure your translation keys exist.
Label resolution order
The label() method resolves the display label in the following priority order:
- Translatable attribute key via __()
- Description attribute text
- Humanized enum value (Str::headline)
2) Helpers
- label(): string
Status::DRAFT->label(); // "Draft" Status::IN_REVIEW->label(); // "In Review"
- values(bool $asArray = false): Collection|array
Status::values(); // Illuminate\Support\Collection of ["draft", "published", "in_review"] Status::values(asArray: true); // ["draft", "published", "in_review"]
- asOptions(string $labelKey = 'label', string $valueKey = 'value', bool $asArray = false): Collection|array
Status::asOptions(); /* Collection like: [ ['label' => 'Draft', 'value' => 'draft'], ['label' => 'Published', 'value' => 'published'], ['label' => 'In Review', 'value' => 'in_review'], ] */ Status::asOptions(labelKey: 'text', valueKey: 'id', asArray: true); /* Array like: [ ['text' => 'Draft', 'id' => 'draft'], ['text' => 'Published', 'id' => 'published'], ['text' => 'In Review', 'id' => 'in_review'], ] */
These can be fed directly into form selects, API responses, etc.
Laravel Integration
- Auto-discovered service provider: Mahmudul\LaraEnum\LaraEnumServiceProvider
- No configuration is required
Development
- Minimum stability is set to "dev" with prefer-stable to allow framework version ranges.
License
MIT License. See the LICENSE file (or the package metadata) for details.
mahmudul/lara-enum 适用场景与选型建议
mahmudul/lara-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 459 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「attributes」 「enum」 「laravel」 「description」 「labels」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mahmudul/lara-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mahmudul/lara-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mahmudul/lara-enum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Enum type behavior and helper for Yii2, for PostgreSQL only
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
统计信息
- 总下载量: 459
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-09