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
ChesterS7mo ago
Cool. What's the problem?
hello_world
hello_worldOP7mo ago
if i have the value as null then i want to change it to '-' but i can't
Tetracyclic
Tetracyclic7mo ago
What's the problem you're running into? What happens if you use: ->formatStateUsing(fn ($state): string => $state ?? '-')
hello_world
hello_worldOP7mo 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
ChesterS7mo 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_worldOP7mo ago
not working bro
Tetracyclic
Tetracyclic7mo 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
LeandroFerreira7mo ago
->placeholder('-')
Dennis Koch
Dennis Koch7mo ago
formatStateUsing is not executed for null values. Try ->placeholder() as Leandro suggests or use ->getStateUsing()
Tetracyclic
Tetracyclic7mo ago
TIL. Should that be PRd to the docs?
Tetracyclic
Tetracyclic7mo 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_worldOP7mo ago
working Thanks @Leandro Ferreira and @Dennis Koch
Want results from more Discord servers?
Add your server