I can't formatState of TextColumn having value null

TextColumn::make('last_contacted')->date('M d')->label('Last Attempt')->sortable()->formatStateUsing(function($state){
if($state != null){
info($state);
}
else{
info('hello');
}
}),
TextColumn::make('last_contacted')->date('M d')->label('Last Attempt')->sortable()->formatStateUsing(function($state){
if($state != null){
info($state);
}
else{
info('hello');
}
}),
14 Replies
ChesterS
ChesterS3mo ago
Cool. What's the problem?
hello_world
hello_world3mo ago
if i have the value as null then i want to change it to '-' but i can't
Tetracyclic
Tetracyclic3mo ago
What's the problem you're running into? What happens if you use: ->formatStateUsing(fn ($state): string => $state ?? '-')
hello_world
hello_world3mo ago
i want a '-' at the arrow mark place in the picture but the code gave isn't working because the value of that place is null so formatStateUsing isn't working there
No description
ChesterS
ChesterS3mo ago
Try to remove the ->date('M d') part and formatting the date inside your formatStateUsing section
TextColumn::make('last_contacted')
->label('Last Attempt')
->sortable()
->formatStateUsing(function($state){
if($state != null){
info($state->format('M d'));
}
else{
info('hello');
}
}),
TextColumn::make('last_contacted')
->label('Last Attempt')
->sortable()
->formatStateUsing(function($state){
if($state != null){
info($state->format('M d'));
}
else{
info('hello');
}
}),
something like that
hello_world
hello_world3mo ago
not working bro
Tetracyclic
Tetracyclic3mo ago
Are you sure that $state is null, and not an empty string? In this: ->formatStateUsing(fn ($state): string => $state ?? '-') If $state is an empty string, it will return that instead. If so, the following should work ->formatStateUsing(fn ($state): string => filled($state) ? $state : '-')
LeandroFerreira
LeandroFerreira3mo ago
->placeholder('-')
Dennis Koch
Dennis Koch3mo ago
formatStateUsing is not executed for null values. Try ->placeholder() as Leandro suggests or use ->getStateUsing()
Tetracyclic
Tetracyclic3mo ago
TIL. Should that be PRd to the docs?
Tetracyclic
Tetracyclic3mo ago
I'm referring to formatStateUsing not being called when a field is null on initialisation, and that getStateUsing() could be an alternative in those situations. getStateUsing() was in the v2 docs, but removed. The docs for formatStateUsing (in forms) only say that it's called when fill() is called on the form, but not that it's only called on fields that will be have data to fill. And dehydrateStateUsing is called on fields with a null value.
hello_world
hello_world3mo ago
working Thanks @Leandro Ferreira and @Dennis Koch