高级
枚举小技巧
简介
枚举是特殊的 PHP 类,表示一组固定的常量。它们对于建模可能值数量有限的概念非常有用,例如一周中的几天、一年中的几个月或一副牌的花色。
由于枚举的 “case” 是枚举类的实例,因此向枚举添加接口非常有用。Filament 提供了一系列可添加到枚举的接口,从而提升你的使用体验。
NOTE
在 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
接口可用于从枚举生成选项数组,其中枚举的值是键,枚举的标签是值。这适用于表单字段,如 Select
和 CheckboxList
,以及表格构建器的 SelectColumn
和 SelectFilter
:
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
,并且在 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',
};
}
}
Using the enum color with a text column in your table
If you use a TextColumn
with the Table Builder, 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 column.
Using the enum color with a text entry in your infolist
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.
Using the enum color with a toggle buttons field in your form
If you use a ToggleButtons
form field, and it is set to use an enum for its options, Filament will automatically use the HasColor
interface to display the enum label in its color.
Enum icons
The HasIcon
interface transforms an enum instance into an icon. This is useful for displaying icons alongside enum values in your 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',
};
}
}
Using the enum icon with a text column in your table
If you use a TextColumn
with the Table Builder, and it is cast to an enum in your Eloquent model, Filament will automatically use the HasIcon
interface to display the enum’s icon aside its label. This works best if you use the badge()
method on the column.
Using the enum icon with a text entry in your infolist
If you use a TextEntry
in an infolist, and it is cast to an enum in your Eloquent model, Filament will automatically use the HasIcon
interface to display the enum’s icon aside its label. This works best if you use the badge()
method on the entry.
Using the enum icon with a toggle buttons field in your form
If you use a ToggleButtons
form field, and it is set to use an enum for its options, Filament will automatically use the HasIcon
interface to display the enum’s icon aside its label.
Enum descriptions
The HasDescription
interface transforms an enum instance into a textual description, often displayed under its label. This is useful for displaying human-friendly descriptions in your 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.',
};
}
}
Using the enum description with form field descriptions
The HasDescription
interface can be used to generate an array of descriptions from an enum, where the enum’s value is the key and the enum’s description is the value. This applies to form fields like Radio
and CheckboxList
:
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Radio;
Radio::make('status')
->options(Status::class)
CheckboxList::make('status')
->options(Status::class)
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion