F
Filamentβ€’15mo 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
JohanOPβ€’15mo ago
Am I missing something here?
awcodes
awcodesβ€’15mo ago
can you dd($state) and confirm it is actually null
Johan
JohanOPβ€’15mo 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
Arkoβ€’15mo ago
Use getStateUsing() instead
Johan
JohanOPβ€’15mo ago
That worked, thanks @Arko
Arko
Arkoβ€’15mo ago
πŸ‘ follow the instructions from @Answer Overflow to mark the solution
Want results from more Discord servers?
Add your server