Skip to main content
Quick Tip

Filament: Format Table Column Value with Custom Helper Method

If you want to format a table column value in a custom way, you can define the formatting method as a helper and then use it with ->formatStateUsing() in the column.

Here's an example from Dinero open-source project:

app/Filament/Resources/WalletResource.php

class WalletResource extends Resource
{
public static function table(Table $table): Table
{
return $table
->columns([
// ... other columns
Tables\Columns\TextColumn::make('currency_code')
->label(__('wallets.fields.currency_code'))
->formatStateUsing(fn (string $state): string => country_with_currency_and_symbol($state))
->sortable(),
]);
}
}

The country_with_currency_and_symbol() method is defined in this file:

app/Support/helpers.php

use Illuminate\Support\Collection;
 
function country_with_currency_and_symbol($state = null): Collection|string {
$countries = collect(countries())->mapWithKeys(function ($country) {
try {
$currency = currency($country['currency']);
return [
$country['currency'] => sprintf('%s - %s - %s (%s)',$country['name'], $currency->getCurrency(), $currency->getName(), $currency->getSymbol())
];
} catch (\Exception $e) {
return [null => null];
}
})->filter();
 
if($state) {
return $countries->get($state);
}
 
return $countries;
}

Then this file is auto-loaded with Composer:

composer.json

{
"autoload": {
"files": [
"app/Support/helpers.php"
]
},
}

Visual result:

Link to the Filament docs

Enjoyed This Tip?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses

[NEW] Laravel AI SDK: 6 Practical Examples

9 lessons
1 h 02 min

Livewire v3 to v4: Changes You Need to Know

7 lessons
31 min

Building a Typical Laravel SaaS

13 lessons
1 h 58 min

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.