FilamentF
Filamentβ€’3y ago
Yasser

SelectColumn - Dynamic options and Icons

Hello there,
once again I am enjoying myself in Filament πŸ˜„
I am trying to create a SelectColumn that allows control over the status of a Post

I tried to add in comments what I am trying to do:
  • I would love to have some Icons instead of the strings at the options
  • I would really like the placeholder to be disabled, yet show the current status based on my logic
Should I use an actiongroup instead ?

Tables\Columns\SelectColumn::make('status')
->options(
    [
        'publish' => 'Publish',//I would love to add an icon here and change the color
        'unPublish' => 'UnPublish',
        'markAsFeatured' => 'Feature',
        'unmarkAsFeatured' => 'Unfeature'
    ]
)->updateStateUsing(function ($state, $record) {
    switch ($state) {
        case 'publish':
            $record->publish();
            break;
        case 'unPublish':
            $record->unpublish();
            break;
        case 'markAsFeatured':
            $record->markAsFeatured();
            break;
        case 'unmarkAsFeatured':
            $record->unmarkAsFeatured();
            break;
    }
}
)->disableOptionWhen(
    function (string $value,$record): bool {
        return match ($value) {
            'publish' => $record->isDraft(),
            'unPublish' => $record->isPublished(),
            'markAsFeatured' => !$record->isFeatured(),
            'unmarkAsFeatured' => $record->isFeatured(),
            default => false,
        };
    }
)->placeholder(function ($state, $record) {
    if ($record->trashed()) {
        return 'heroicon-s-trash';
    }
    if ($record->isFeatured()) {
        return 'heroicon-s-star';
    }
    if ($record->isPublished()) {
        return 'heroicon-s-globe-europe-africa';
    }
    if ($record->isDraft()) {
        return 'heroicon-s-pencil';
    }
    return 'heroicon-s-question-mark-circle';
})
->disabled(fn($state, $record) => $record->trashed()),
Was this page helpful?