Skip to main content
Tutorial Free

Filament Table Null Value: formatStateUsing() Doesn't Work?

May 08, 2024
1 min read

In Filament tables, some values could be null, but you may want to still show some placeholder instead? A typical formatStateUsing() method wouldn't work. Let me show two alternatives.

Usually, when formatting the state, you would use the formatStateUsing() method:

TextColumn::make('status')
->formatStateUsing(fn (string $state): string => __("statuses.{$state}"))

But when the $state is null, the formatStateUsing() method is not executed. There are two ways to deal with this case.


Option 1: default()

The first way is to use the default() method on the column.

Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('description')
->default('No description'),

When using this method, Filament treats it as a state and also sets columns like image or color with the default values. This is different from the second alternative - placeholder.


Option 2: placeholder()

The second option is to add a placeholder.

Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('description')
->placeholder('No description'),

Placeholder has a lighter gray text color and isn't treated as a state.


If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.

Enjoyed This Tutorial?

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

Recent Courses

Comments & Discussion

EV
Eugene van der Merwe ✓ Link copied!

I just spend an hour trying to see why formatStateUsing doesn't work! Thanks for this tip.

MM
Mohamed Mizhar (Raja) ✓ Link copied!

You've truly saved my day! I spent nearly 5 hours trying to fix this. I really appreciate your help!

WM
Wallace Maxters ✓ Link copied!

Another case is when you try the format the NULL with a icon and filled string with another. See:

TextColumn::make('has_url') // not exists in database
->default(fn ($record) => $record->pivot->url !== null)
->formatStateUsing(fn ($state) => $state ? 'Yes' : 'No')
->label('Has Link')
->color(fn ($state) => $state ? 'primary' : 'gray')
->icon(fn ($state) => $state ? 'heroicon-o-link' : 'heroicon-o-document-text');

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.