F
Filament15mo ago
skyrowl

Query JSON columns in filament tables

Hey, is it possible to query JSON columns in tables ? I've been trying multiple things like ->, ., [] without any succes Is it possible to do something like this Tables\Columns\TextColumn::make('attribute_data->name->value') ?
11 Replies
awcodes
awcodes15mo ago
I believe it needs to be a relationship for dot syntax to work. But you can use ->formatStateUsing() to pull the values you need. https://filamentphp.com/docs/2.x/tables/columns/text#custom-formatting
Filament
Text column - Columns - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
skyrowl
skyrowl15mo ago
formatStateUsing was what I was thinking of thanks for confirmation 😄 tho what if I need two values on the same JSON column ?
awcodes
awcodes15mo ago
If they’re in the same column you can concatenate them into a string and just return it. Could even use HtmlString to output more complicated structure like ul’s
skyrowl
skyrowl15mo ago
isn't there a way to pull the two values in different columns ?
awcodes
awcodes15mo ago
Maybe, not sure how to do it though since technically a table column represents a database column. Maybe accessors on the model or virtual columns.
skyrowl
skyrowl15mo ago
How would you setup virtual columns ? I'm aware of accessors but not virtual columns it confuses me a bit sorry 😅 Oh I searched I'm sorry I knew that for MySQL I was thinking you were talking about Laravel 😓
awcodes
awcodes15mo ago
Yea. It’s not a Filament thing. But it works if you set them up right. Not sure how a virtual column would work with json data though. Never even thought about it.
skyrowl
skyrowl15mo ago
Pretty think accessors would be easier to setup and more efficient
awcodes
awcodes15mo ago
I don’t disagree.
skyrowl
skyrowl15mo ago
Thanks for your help 😁
awcodes
awcodes15mo ago
No worries.