枚举是特殊的 PHP 类,代表一组固定的常量。它们对于建模具有有限可能值的概念很有用,例如一周中的几天、一年中的月份,或者一副牌中的花色。
由于枚举的“case”是枚举类的实例,为枚举添加接口被证明非常有用。Filament 提供一系列可添加到枚举中的接口,这将增强你在使用它们时的体验。
当在您的 Eloquent 模型上使用带有属性的枚举时,请确保其被正确地进行类型转换。
HasLabel 接口将枚举实例转换为文本标签。这对于在您的 UI 中显示易读的枚举值很有用。
use Filament\Support\Contracts\HasLabel;
enum Status: string implements HasLabel
{
case Draft = 'draft';
case Reviewing = 'reviewing';
case Published = 'published';
case Rejected = 'rejected';
public function getLabel(): ?string
{
return $this->name;
// or
return match ($this) {
self::Draft => 'Draft',
self::Reviewing => 'Reviewing',
self::Published => 'Published',
self::Rejected => 'Rejected',
};
}
}HasLabel 接口可用于从枚举生成一个选项数组,其中枚举的值作为键,枚举的标签作为值。这适用于表单字段,例如 选择 和 复选框列表,以及表构建器的 选择列 和 选择过滤器:
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\SelectColumn;
use Filament\Tables\Filters\SelectFilter;
Select::make('status')
->options(Status::class)
CheckboxList::make('status')
->options(Status::class)
Radio::make('status')
->options(Status::class)
SelectColumn::make('status')
->options(Status::class)
SelectFilter::make('status')
->options(Status::class)在这些示例中,Status::class 是实现了 HasLabel 的枚举类,并且选项是从该枚举类生成的:
[
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
'rejected' => 'Rejected',
]如果您使用 TextColumn 与 Table Builder 配合使用,并且它在您的 Eloquent 模型中被转换为枚举类型时,Filament 将会自动使用 HasLabel 接口来显示枚举的标签,而不是其原始值。
如果您在表格构建器中使用分组,并且它在您的 Eloquent 模型中被转换为枚举,Filament 将自动使用 HasLabel 接口来显示枚举的标签,而不是其原始值。该标签将显示为每个组的标题。
如果您在信息列表中使用TextEntry,并且它在您的 Eloquent 模型中被转换为枚举类型时,Filament 将自动使用HasLabel接口来显示该枚举的标签,而非其原始值。
该HasColor 接口将枚举实例转换为一个颜色。这对于在您的 UI 中显示带颜色的枚举值很有用。
use Filament\Support\Contracts\HasColor;
enum Status: string implements HasColor
{
case Draft = 'draft';
case Reviewing = 'reviewing';
case Published = 'published';
case Rejected = 'rejected';
public function getColor(): string | array | null
{
return match ($this) {
self::Draft => 'gray',
self::Reviewing => 'warning',
self::Published => 'success',
self::Rejected => 'danger',
};
}
}color 与文本列一同使用如果你使用 TextColumn 与 Table Builder 配合使用时,并且在你的 Eloquent 模型中被转换为枚举类型,Filament 将自动使用 HasColor 接口,以其颜色显示枚举标签。这种方法效果最好,如果你在该列上使用 badge() 方法。
If you use a TextEntry in an infolist, and it is cast to an enum in your Eloquent model, Filament will automatically use the HasColor interface to display the enum label in its color. This works best if you use the badge() method on the entry.
如果你使用一个 ToggleButtons 表单字段,并且其选项设置为使用枚举,Filament 将自动使用 HasColor 接口以其颜色显示枚举标签。
HasIcon 接口将枚举实例转换为一个图标。这对于在您的 UI 中与枚举值一起显示图标很有用。
use Filament\Support\Contracts\HasIcon;
enum Status: string implements HasIcon
{
case Draft = 'draft';
case Reviewing = 'reviewing';
case Published = 'published';
case Rejected = 'rejected';
public function getIcon(): ?string
{
return match ($this) {
self::Draft => 'heroicon-m-pencil',
self::Reviewing => 'heroicon-m-eye',
self::Published => 'heroicon-m-check',
self::Rejected => 'heroicon-m-x-mark',
};
}
}如果你使用 TextColumn 与 Table Builder 一起,并且在你的 Eloquent 模型中被转换为枚举,Filament 将自动使用 HasIcon 接口来在其标签旁边显示该枚举的图标。如果你在该列上使用 badge() 方法,这种效果最佳。
如果您在信息列表中使用TextEntry,并且在您的Eloquent模型中将其转换为枚举,Filament将自动使用HasIcon接口来在其标签旁边显示枚举的图标。此功能效果最佳如果您在条目上使用badge()方法。
如果你使用一个 ToggleButtons 表单字段,并且它被设置为使用枚举作为其选项,Filament 将自动使用 HasIcon 接口来在其标签旁边显示枚举的图标。
HasDescription 接口将枚举实例转换为文本描述,通常显示在其 标签 下方。这对于在您的 UI 中显示人性化的描述很有用。
use Filament\Support\Contracts\HasDescription;
use Filament\Support\Contracts\HasLabel;
enum Status: string implements HasLabel, HasDescription
{
case Draft = 'draft';
case Reviewing = 'reviewing';
case Published = 'published';
case Rejected = 'rejected';
public function getLabel(): ?string
{
return $this->name;
}
public function getDescription(): ?string
{
return match ($this) {
self::Draft => 'This has not finished being written yet.',
self::Reviewing => 'This is ready for a staff member to read.',
self::Published => 'This has been approved by a staff member and is public on the website.',
self::Rejected => 'A staff member has decided this is not appropriate for the website.',
};
}
}HasDescription 接口可用于从枚举中生成描述数组,其中枚举值为键,枚举描述为值。这适用于表单字段,例如 单选框 和 复选框列表:
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Radio;
Radio::make('status')
->options(Status::class)
CheckboxList::make('status')
->options(Status::class)