Fields

Enum

Basics

Inherits from Select.

* has the same capabilities.

Operates the same as the Select field but accepts Enum as options.

The model attributes require Enum Cast.

Creation

use MoonShine\UI\Fields\Enum;
 
Enum::make('Status')
->attach(StatusEnum::class)

Displaying Values

toString

The toString() method implemented in Enum allows you to set the displayed value.

namespace App\Enums;
 
enum StatusEnum: string
{
case NEW = 'new';
case DRAFT = 'draft';
case PUBLIC = 'public';
 
public function toString(): ?string
{
return match ($this) {
self::NEW => 'New',
self::DRAFT => 'Draft',
self::PUBLIC => 'Public',
};
}
}

Color

If the Enum implements the getColor() method, the preview field will be displayed as an icon of a specific color.

Available colors:

primary secondary success warning error info

purple pink blue green yellow red gray

namespace App\Enums;
 
enum StatusEnum: string
{
case NEW = 'new';
case DRAFT = 'draft';
case PUBLIC = 'public';
 
public function getColor(): ?string
{
return match ($this) {
self::NEW => 'info',
self::DRAFT => 'gray',
self::PUBLIC => 'success',
};
}
}

enum

enum_dark