phplaravellaravel-filamentfilamentphp

Tables\Columns\IconColumns use a fn call to alternate icons depending on value but uses LIKE, would like to use a LIKE or CONTAINS


I am using Filament admin panel in Laravel. Filament v3.3.14, Laravel 12x

I have found this great little function to alternate my icons depending on the value (0 or 1).

             Tables\Columns\IconColumn::make('paid')
                ->icon(fn (string $state): string => match ($state) {
                    '1' => 'heroicon-o-check-circle',
                    '0' => 'heroicon-o-x-circle',
                })

I would like to use it on a different function with text values as below:

Tables\Columns\IconColumn::make('test_value')
                ->icon(fn (string $state): string => match ($state) {
                    'high' => 'heroicon-o-check-circle',
                    'medium' => 'heroicon-o-x-circle',
                    'low' => 'heroicon-o-x-circle',
                    'extreme' => 'heroicon-o-x-circle',
                })

But the test_value will often end up with values like "Medium (12)" or "Extreme (25)".

Is there a way to loosen up the string match? Similar to a LIKE or CONTAINS?

I cannot locate any direction out in the wide yonder internet. Would greatly appreciate if you have any suggestions.


Solution

  • You can do match(true) and it will return the first expression that evaluates to true. then you can use whatever fuzzy matching logic you like, for example:

    fn(string $state): string => match (true) {
        str_starts_with("High", $state) => 'heroicon-o-check-circle',
        str_starts_with("medium", strtolower($state)) => 'heroicon-o-x-circle',
        $state === 'low' => 'heroicon-o-x-circle',
        stripos($state, 'Extreme') !== false => 'heroicon-o-x-circle',
    }