Enum color in DB

What is the best way to save the colors of an enum in the db, so that the administrator can change the color?
...
public function getColor(): string|array|null
{
return match ($this) {
self::Ordinato => Color::Cyan,
self::Eseguito => Color::Blue,
self::Verificato => Color::Yellow,
};
}
...
public function getColor(): string|array|null
{
return match ($this) {
self::Ordinato => Color::Cyan,
self::Eseguito => Color::Blue,
self::Verificato => Color::Yellow,
};
}
class Color extends Model
{
protected $table = 'colors';
protected $fillable = ['name', 'value'];
}
...
public function getColor(): string|array|null
{
return match ($this) {
self::Ordinato => Color::where('name', self::Ordinato)->first()->value,
self::Eseguito => Color::where('name', self::Eseguito)->first()->value,
self::Verificato => Color::where('name', self::Verificato)->first()->value,
};
}
class Color extends Model
{
protected $table = 'colors';
protected $fillable = ['name', 'value'];
}
...
public function getColor(): string|array|null
{
return match ($this) {
self::Ordinato => Color::where('name', self::Ordinato)->first()->value,
self::Eseguito => Color::where('name', self::Eseguito)->first()->value,
self::Verificato => Color::where('name', self::Verificato)->first()->value,
};
}
2 Replies
Marco Mapelli
Marco MapelliOP10mo ago
If I save the cache value I shouldn’t have slowdowns because it goes to read every time in the db, right?
Saade
Saade10mo ago
Save only the name of the selected color in the database and create a custom model cast that casts it into your enum perhaps, see if this is what you want https://github.com/saade/filament-extra?tab=readme-ov-file#colorselect https://github.com/saade/filament-extra?tab=readme-ov-file#class-color
Want results from more Discord servers?
Add your server