F
Filament10mo ago
ocram82

Enum TextColumn badge() with label, icon and color

I have this enum class
<?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',
};
}
}
and in my Resurce i've
Tables\Columns\TextColumn::make('accepted_status')
->label('Status')
->badge()
->sortable(),
Tables\Columns\TextColumn::make('accepted_status')
->label('Status')
->badge()
->sortable(),
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
4 Replies
ocram82
ocram8210mo ago
Trying to do something while waiting for a response i solved doing that:
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(),
thanks to all and hope this is useful for others
Jakub
Jakub6mo ago
what did you put in for getLabel? can you show the enum example, for the getLabel method
Pathros
Pathros3w ago
But I get the following error:
SubstitutionStatusEnum::from(): Argument #1 ($value) must be of type string|int, SubstitutionStatusEnum given
Well, 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()
,
That did the trick for me
ocram82
ocram822w ago
this is the complete code of the enum:
<?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',
};
}
}
happy to hear that 👍