is there a formatStateUsing for IconColumn?

What I have: I have a table with a one-to-many boolean icon column (positionData.is_active) Currently the State is populates with all the records for example (true, false, true) What I want to do: I just want a single value returned. What I have tried If it was a TextColumn I could use something like
TextColumn::make('positionData.is_active)
->formatStateUsing(
function ($record) {
$isActive = $record->positionData->sortByDesc('year')->values()->first()->is_active;
// render stuff here ...
}
TextColumn::make('positionData.is_active)
->formatStateUsing(
function ($record) {
$isActive = $record->positionData->sortByDesc('year')->values()->first()->is_active;
// render stuff here ...
}
But this doesnt seem to be a thing for IconColumns. I've tried using the
TextColumn::make('positionData.is_active)->icon(...)
TextColumn::make('positionData.is_active)->icon(...)
But the format and size is totally different than the Icon Columns. Any suggestions?
Solution:
```php Tables\Columns\IconColumn::make('position_data_active') ->boolean() ->state(fn (YourModel $record): bool => $record ->positionData...
Jump to solution
5 Replies
403gtfo
403gtfoOP8mo ago
Do you mean withing the TextColumn->formatStateUsing() ? I can, but the TexColumns dont seem to render icons the same as IconColumns See attached. If I use the IconColumn, I cant seem to filter out one of the one to many icons (Does that make sense?) See attached.
No description
No description
Povilas K
Povilas K8mo ago
@403gtfo haven't tried it in Filament but maybe you should use oneOfMany instead of oneToMany? Like latestOfMany and similar? Then you would get one record from Eloquent: https://laravel.com/docs/11.x/eloquent-relationships#has-one-of-many
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Solution
LeandroFerreira
LeandroFerreira8mo ago
Tables\Columns\IconColumn::make('position_data_active')
->boolean()
->state(fn (YourModel $record): bool => $record
->positionData
->sortByDesc('year')
->first()
->is_active ?? false
)
Tables\Columns\IconColumn::make('position_data_active')
->boolean()
->state(fn (YourModel $record): bool => $record
->positionData
->sortByDesc('year')
->first()
->is_active ?? false
)
?
403gtfo
403gtfoOP8mo ago
As always you set me off on the right path 😄 I couldnt get the morph to work (I'll have to go watch your videos on it again to better wrap my head around it.) But I did just make another basic relation.

public function latestPositionData(){
return $this->hasOne(PositionData::class)->latestOfMany('year');
}

public function latestPositionData(){
return $this->hasOne(PositionData::class)->latestOfMany('year');
}
Then in the filament table I can just use:
IconColumn::make('latestPositionData.is_participating')->boolean()->sortable(),
IconColumn::make('latestPositionData.is_participating')->boolean()->sortable(),
Works like a charm! Thanks again. Love your work & videos. 😮 that is handy to know, thanks man! Added yours as the solution to the question I asked. But found I could do a simple extra model relation and it worked even better then modifying the state.
Want results from more Discord servers?
Add your server