guan-changhu / enum
guan-changhu enum
dev-master
2025-10-26 11:55 UTC
Requires
- php: ^8.1
- spatie/laravel-enum: dev-main
This package is auto-updated.
Last update: 2025-10-26 11:55:51 UTC
README
package enum
创建枚举类
<?php namespace GuanChanghu\Enum; /** * @method static self underway() * @method static self success() * @method static self fail() */ class StatusEnum extends BaseEnum { protected static function values(): array { return [ 'underway' => self::STATUS_UNDERWAY, 'success' => self::STATUS_SUCCESS, 'fail' => self::STATUS_FAIL, ]; } protected static function labels(): array { return [ 'underway' => __('enum.status.underway'), 'success' => __('enum.status.success'), 'fail' => __('enum.status.fail'), ]; } public const STATUS_UNDERWAY = 0; public const STATUS_SUCCESS = 1; public const STATUS_FAIL = 2; }
2.创建语言文件 在 resources/lang/zh_CN/enum.php 中添加
<?php return [
'status' => [
'underway' => '进行中',
'success' => '成功',
'fail' => '失败',
],
];
3.使用多语言枚举
<?php // 获取当前语言的标签 $label = StatusEnum::underway()->label;
// 获取所有选项用于下拉框 $options = StatusEnum::toSelectArray();
或者
<?php
StatusEnum::enum('underway')->label;