Fields

Enum

Basics

Inherits from Select.

* has the same capabilities.

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

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

The model attributes require Enum Cast.

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',
};
}
}
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 Enum implements the getColor() method, the field in "preview" mode 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',
};
}
}
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',
};
}
}

Icon

If Enum implements the getIcon() method, an icon will be displayed next to the value in "preview" mode.

To display the icon, the getColor() method must also be implemented.

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',
};
}
 
public function getIcon(): ?string
{
return match ($this) {
self::NEW => 'sparkles',
self::DRAFT => 'pencil',
self::PUBLIC => 'check-circle',
};
}
}
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',
};
}
 
public function getIcon(): ?string
{
return match ($this) {
self::NEW => 'sparkles',
self::DRAFT => 'pencil',
self::PUBLIC => 'check-circle',
};
}
}

A list of all available icons can be found in the Icons section.