关于 laravel-enums
laravel-enums 是面向 Laravel 开发者的开发技能,用于定义类型安全的有限值集合。内容介绍 backed enum、可复用行为,以及在模型、DTO、验证和队列中的用法。
主要功能
- 01Backed enum 模式
- 02枚举方法与 trait
- 03模型类型转换
- 04DTO 属性
- 05枚举验证
- 06队列值用法
适用场景
- 01表示订单状态
- 02跨枚举共享标签
- 03验证枚举输入
- 04将任务分配到命名队列
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ leeovery/ agentic-skills --skill laravel-enums安装后在 Claude Code 中输入/laravel-enums确认已加载。
详细介绍
Laravel 枚举
枚举提供类型安全的有限值集合。
相关指南:
- 状态机 - 用于复杂的状态转换
- 模型 - 模型将值转换为枚举
- DTO - 带有枚举属性的 DTO
- form-requests.md - 枚举验证
始终使用有底层值的枚举
始终使用有底层值的枚举(字符串或整数):
<?php
declare(strict_types=1);
namespace App\Enums;
enum OrderStatus: string
{
case Pending = 'pending';
case Processing = 'processing';
case Completed = 'completed';
case Cancelled = 'cancelled';
}
扩展枚举
按需利用枚举上的方法和 trait 添加功能。将共享行为放在 app/Enums/Concerns/ trait 中。
// Direct methods for one-off behavior
enum OrderStatus: string
{
case Pending = 'pending';
case Completed = 'completed';
public function label(): string
{
return match ($this) {
self::Pending => 'Pending Review',
self::Completed => 'Completed',
};
}
public function color(): string
{
return match ($this) {
self::Pending => 'yellow',
self::Completed => 'green',
};
}
}
// Reusable trait for shared behavior across enums
trait HasLabel
{
abstract public function label(): string;
public static function labels(): array
{
return collect(self::cases())->mapWithKeys(
fn ($enum) => [$enum->value => $enum->label()]
)->toArray();
}
}
枚举中的业务逻辑
枚举可以通过 match 表达式包含行为:
enum PaymentProvider: string
{
case Stripe = 'stripe';
case PayPal = 'paypal';
case Square = 'square';
public function processingFee(int $amount): int
{
return match ($this) {
self::Stripe => (int) ($amount * 0.029 + 30),
self::PayPal => (int) ($amount * 0.034 + 30),
self::Square => (int) ($amount * 0.026 + 10),
};
}
public function supportsRefunds(): bool
{
return match ($this) {
self::Stripe, self::PayPal => true,
self::Square => false,
};
}
}
在模型中使用
protected function casts(): array
{
return [
'status' => OrderStatus::class,
'payment_method' => PaymentMethod::class,
];
}
在 DTO 中使用
public function __construct(
public OrderStatus $status,
public PaymentMethod $paymentMethod,
) {}
在验证中使用
use Illuminate\Validation\Rules\Enum;
'status' => [
'required',
'string',
'bail',
new Enum(OrderStatus::class),
],
常见模式
Match 表达式
$message = match ($order->status) {
OrderStatus::Pending => 'Your order is pending',
OrderStatus::Processing => 'We are processing your order',
OrderStatus::Completed => 'Your order is complete',
OrderStatus::Cancelled => 'Your order was cancelled',
};
队列枚举示例
<?php
declare(strict_types=1);
namespace App\Enums;
enum Queue: string
{
case Default = 'default';
case Processing = 'processing';
case Emails = 'emails';
case Notifications = 'notifications';
}
在任务中使用:
public function __construct(public Order $order)
{
$this->onQueue(Queue::Emails->value);
}
目录结构
app/Enums/
├── OrderStatus.php
├── PaymentMethod.php
└── Queue.php
何时使用枚举,何时使用状态机
使用枚举:
- 简单的状态字段
- 无转换逻辑
- 无副作用
使用状态机:
- 带规则的复杂状态转换
- 特定状态的行为
- 转换副作用
有关状态机的信息,请参阅状态机。
总结
枚举提供:
- 类型安全
- 有限值集合
- 业务逻辑封装
- UI 辅助功能(标签、颜色、图标)
- IDE 自动补全
始终使用有底层值的枚举,其值为字符串或整数。
评论
支持 Markdown