Enum TextColumn badge() with label, icon and color
I have this enum class
and in my Resurce i've
but i only see a badge right coloured with enum value (suspended, accepted, ecc...) but i can't see the icon and the label. How can i get the labels and the icon inside the badge?
Thanks
<?php
namespace App\Enums;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
enum SubstitutionStatusEnum: string implements HasColor, HasIcon, HasLabel
{
case Suspended = 'suspended';
case Accepted = 'accepted';
case Rejected = 'rejected';
public function getColor(): string | array | null
{
return match ($this) {
self::Suspended => 'primary',
self::Accepted => 'success',
self::Rejected => 'danger',
};
}
public function getIcon(): ?string
{
return match ($this) {
self::Suspended => 'heroicon-o-ellipsis-horizontal-circle',
self::Accepted => 'heroicon-o-check-circle',
self::Rejected => 'heroicon-o-x-circle',
};
}
public function getLabel(): ?string
{
return match ($this) {
self::Suspended => 'Suspended Acceptance',
self::Accepted => 'Accepted Acceptance',
self::Rejected => 'Rejected Acceptance',
};
}
}
<?php
namespace App\Enums;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
enum SubstitutionStatusEnum: string implements HasColor, HasIcon, HasLabel
{
case Suspended = 'suspended';
case Accepted = 'accepted';
case Rejected = 'rejected';
public function getColor(): string | array | null
{
return match ($this) {
self::Suspended => 'primary',
self::Accepted => 'success',
self::Rejected => 'danger',
};
}
public function getIcon(): ?string
{
return match ($this) {
self::Suspended => 'heroicon-o-ellipsis-horizontal-circle',
self::Accepted => 'heroicon-o-check-circle',
self::Rejected => 'heroicon-o-x-circle',
};
}
public function getLabel(): ?string
{
return match ($this) {
self::Suspended => 'Suspended Acceptance',
self::Accepted => 'Accepted Acceptance',
self::Rejected => 'Rejected Acceptance',
};
}
}
Tables\Columns\TextColumn::make('accepted_status')
->label('Status')
->badge()
->sortable(),
Tables\Columns\TextColumn::make('accepted_status')
->label('Status')
->badge()
->sortable(),
4 Replies
Trying to do something while waiting for a response i solved doing that:
thanks to all and hope this is useful for others
Tables\Columns\TextColumn::make('accepted_status')
->label('Stato')
->badge()
->icon(fn (Substitution $record) => SubstitutionStatusEnum::from($record->accepted_status)->getIcon())
->color(fn (Substitution $record) => SubstitutionStatusEnum::from($record->accepted_status)->getColor())
->formatStateUsing(fn (Substitution $record) => SubstitutionStatusEnum::from($record->accepted_status)->getLabel())
->sortable(),
Tables\Columns\TextColumn::make('accepted_status')
->label('Stato')
->badge()
->icon(fn (Substitution $record) => SubstitutionStatusEnum::from($record->accepted_status)->getIcon())
->color(fn (Substitution $record) => SubstitutionStatusEnum::from($record->accepted_status)->getColor())
->formatStateUsing(fn (Substitution $record) => SubstitutionStatusEnum::from($record->accepted_status)->getLabel())
->sortable(),
what did you put in for getLabel?
can you show the enum example, for the getLabel method
But I get the following error:
That did the trick for me
SubstitutionStatusEnum::from(): Argument #1 ($value) must be of type string|int, SubstitutionStatusEnum givenWell, now I have fixed it like so:
<?php
//...
public static function table(Table $table): Table
{
return $table
->columns([
//...
Tables\Columns\TextColumn::make('accepted_status')
->label('Stato')
->badge()
->icon(fn(Substitution $record) => $record->accepted_status->getIcon())
->color(fn(Substitution $record) => $record->accepted_status->getColor())
->formatStateUsing(fn(Substitution $record) => $record->accepted_status->getLabel())
->sortable()
,
<?php
//...
public static function table(Table $table): Table
{
return $table
->columns([
//...
Tables\Columns\TextColumn::make('accepted_status')
->label('Stato')
->badge()
->icon(fn(Substitution $record) => $record->accepted_status->getIcon())
->color(fn(Substitution $record) => $record->accepted_status->getColor())
->formatStateUsing(fn(Substitution $record) => $record->accepted_status->getLabel())
->sortable()
,
this is the complete code of the enum:
happy to hear that 👍
<?php
namespace App\Enums;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
enum SubstitutionStatusEnum: string implements HasColor, HasIcon, HasLabel
{
case Suspended = 'suspended';
case Accepted = 'accepted';
case Rejected = 'rejected';
public function getColor(): string|array|null
{
return match ($this) {
self::Suspended => 'primary',
self::Accepted => 'success',
self::Rejected => 'danger',
};
}
public function getIcon(): ?string
{
return match ($this) {
self::Suspended => 'heroicon-o-ellipsis-horizontal-circle',
self::Accepted => 'heroicon-o-check-circle',
self::Rejected => 'heroicon-o-x-circle',
};
}
public function getLabel(): ?string
{
return match ($this) {
self::Suspended => 'Sospesa',
self::Accepted => 'Accettata',
self::Rejected => 'Rifiutata',
};
}
}
<?php
namespace App\Enums;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
enum SubstitutionStatusEnum: string implements HasColor, HasIcon, HasLabel
{
case Suspended = 'suspended';
case Accepted = 'accepted';
case Rejected = 'rejected';
public function getColor(): string|array|null
{
return match ($this) {
self::Suspended => 'primary',
self::Accepted => 'success',
self::Rejected => 'danger',
};
}
public function getIcon(): ?string
{
return match ($this) {
self::Suspended => 'heroicon-o-ellipsis-horizontal-circle',
self::Accepted => 'heroicon-o-check-circle',
self::Rejected => 'heroicon-o-x-circle',
};
}
public function getLabel(): ?string
{
return match ($this) {
self::Suspended => 'Sospesa',
self::Accepted => 'Accettata',
self::Rejected => 'Rifiutata',
};
}
}