sakanjo/laravel-easy-enum
Composer 安装命令:
composer require sakanjo/laravel-easy-enum
包简介
Easily work with enum.
关键字:
README 文档
README
Easily work with enums.
✨ Help support the maintenance of this package by sponsoring me.
Designed to work with Laravel, Filament, and more.
Table of Contents
📦 Install
composer require sakanjo/laravel-easy-enum
🦄 Usage
1. Create enum
<?php namespace App\Enums; use SaKanjo\EasyEnum; enum ExampleEnum: int { use EasyEnum; case Active = 0; case NOPE = 1; }
2. Create lang file
// lang/en/enums.php <?php use App\Enums; return [ Enums\ExampleEnum::class => [ Enums\ExampleEnum::NOPE->name => 'Nope', // ... ], // ... ];
That's it!
📚 Methods
getLabel
Returns the label of the enum value.
Status::Active->getLabel(); // Active
translated
Returns the translated label of the enum value.
Status::Active->translated('tr'); // Aktif
is
Checks if the enum is equal to another one.
$enum1->is($enum2); // boolean
isNot
inverse of is.
$enum1->isNot($enum2); // boolean
in
Checks if the enum is in a list of enums.
$enum->in([$enum1, $enum2]); // boolean
notIn
inverse of in.
$enum->notIn([$enum1, $enum2]); // boolean
tryFromName
Safely converts a string to its corresponding enum value (returns null if not found).
Status::tryFromName('Active'); // Status::Active Status::tryFromName('Oops'); // null
Converts a string to its corresponding enum value (throws exception if not found).
fromName
Status::fromName('Active'); // Status::Active Status::fromName('Oops'); // Throws ValueError exception
names
Returns a list of case names.
Status::names(); // ['Active', 'NOPE']
values
Returns a list of case values .
Status::values(); // [0, 1]
options
Returns an associative array of case names and values.
Status::options(); // ['Active' => 0, 'NOPE' => 1] Status::options(true); // ['Active' => 0, 'Nope' => 1]
toHtml
alias for getLabel, useful in blade.
Status::Active->toHtml(); // Active
resolveDisplayableValue
same as toHtml except it doesn't render HTML.
Status::Active->resolveDisplayableValue(); // Active
🔥 Practical examples
Filamentphp
Enum
<?php namespace App\Enums; use Filament\Support\Contracts\HasLabel; use SaKanjo\EasyEnum; enum Status: int implements HasLabel { use EasyEnum; case Active = 0; case Disabled = 1; }
Resource
<?php use Filament\Forms; use App\Enums; Forms\Components\Select::make('status') ->options(Enums\Status::class);
Laravel blade
<?php namespace App\Enums; use Illuminate\Contracts\Support\DeferringDisplayableValue; use Illuminate\Contracts\Support\Htmlable; use SaKanjo\EasyEnum; enum Status: int implements Htmlable // or DeferringDisplayableValue { use EasyEnum; case Active = 0; case Disabled = 1; }
<div>
Current status: {{ auth()->user()->status }}
</div>
💖 Support the development
Do you like this project? Support it by donating
Click the "💖 Sponsor" at the top of this repo.
©️ Credits
📄 License
MIT License © 2023-PRESENT Salah Kanjo
sakanjo/laravel-easy-enum 适用场景与选型建议
sakanjo/laravel-easy-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「enums」 「laravel-easy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sakanjo/laravel-easy-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sakanjo/laravel-easy-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sakanjo/laravel-easy-enum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enums for Laravel Enso
Shared backed enums classifying HTML elements by category (block, inline, list, table, void, metadata, root) for the UI Awesome ecosystem.
Helpers for handling enums and enum translations in Laravel
A Laravel package that adds extra power to your PHP enums, and lets you use them in your frontend with type definitions.
A shared set of open-source enums for frequently used domains, helping reduce duplication and improve consistency across software projects.
Production-ready PHP SDK for Iberbanco API v2 - Enterprise banking platform with 80+ currencies, professional enums, type-safe DTOs, optimized performance, and comprehensive financial services
统计信息
- 总下载量: 1.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-19
