F
Filament17mo ago
Johan

formatStateUsing not called if value is null

Tables\Columns\TextColumn::make('cancelled_at')
->label('Cancelled')
->formatStateUsing(fn (?string $state) => $state ? 'Yes' : 'No')
->colors([
'success' => static fn (?string $state): bool => empty($state),
'danger' => static fn (?string $state): bool => !empty($state),
]),
Tables\Columns\TextColumn::make('cancelled_at')
->label('Cancelled')
->formatStateUsing(fn (?string $state) => $state ? 'Yes' : 'No')
->colors([
'success' => static fn (?string $state): bool => empty($state),
'danger' => static fn (?string $state): bool => !empty($state),
]),
This does echo "Yes" but if cancelled_at is null it wont show "No".
Solution:
Use getStateUsing() instead
Jump to solution
6 Replies
Johan
JohanOP17mo ago
Am I missing something here?
awcodes
awcodes17mo ago
can you dd($state) and confirm it is actually null
Johan
JohanOP17mo ago
It is not calling formatStateUsing if the $state is null It is only calling it for the ones which are not null
Solution
Arko
Arko17mo ago
Use getStateUsing() instead
Johan
JohanOP17mo ago
That worked, thanks @Arko
Arko
Arko17mo ago
👍 follow the instructions from @Answer Overflow to mark the solution

Did you find this page helpful?