F
Filamentβ€’8mo ago
ouch

How to show array key in TextColumn

Hi i have Variant model whre i save json in format like this {"Name":"Value"} and i'm casting it
protected $casts = [
'variant_attributes_json' => 'array',
];
protected $casts = [
'variant_attributes_json' => 'array',
];
How could i display key into textcolumn or other column? Because if i put it into textCol it in state is only value
Tables\Columns\TextColumn::make('variant_attributes_json')
Tables\Columns\TextColumn::make('variant_attributes_json')
ps: I'm new to filament πŸ™‚
Solution:
Try ->getStateUsing(fn ($record) => $record->variant_attributes_json) then
Jump to solution
6 Replies
Dennis Koch
Dennis Kochβ€’8mo ago
You can use ->formatStateUsing(fn ($state) => ...) to control the display.
ouch
ouchβ€’8mo ago
Yeah i tried it in the meantime however state gets me the value not the whole array and I'm confused πŸ˜…
Tables\Columns\TextColumn::make('variant_attributes_json')
->formatStateUsing(function ($state): string {
// dd($state);
if (!empty($state && is_array($state))) {
$key = key($state);
$value = $state[$key];
return $key . ' - ' . $value;
}
return $state;
})
Tables\Columns\TextColumn::make('variant_attributes_json')
->formatStateUsing(function ($state): string {
// dd($state);
if (!empty($state && is_array($state))) {
$key = key($state);
$value = $state[$key];
return $key . ' - ' . $value;
}
return $state;
})
Solution
Dennis Koch
Dennis Kochβ€’8mo ago
Try ->getStateUsing(fn ($record) => $record->variant_attributes_json) then
ouch
ouchβ€’8mo ago
Yeah $record works, thanks
CLT99
CLT99β€’8mo ago
Not work for me (in infolist and table)
Dennis Koch
Dennis Kochβ€’8mo ago
Please open an help thread with some info then