F
Filament4mo ago
Dima

How to show only a single icon or boolean on an array column in table view

A record may have multiple files uploaded that are stored in DB column attachments as an array. In the table list I want to show a single icon if the record has any attachments at all. However, the code below shows a list of icons for each of the item of the array in the attachments column.
Tables\Columns\IconColumn::make('attachments')->icon('heroicon-o-photo'),

// The same is with boolean, please see the attached screenshot
Tables\Columns\IconColumn::make('attachments')->boolean(),
Tables\Columns\IconColumn::make('attachments')->icon('heroicon-o-photo'),

// The same is with boolean, please see the attached screenshot
Tables\Columns\IconColumn::make('attachments')->boolean(),
No description
No description
3 Replies
tim.lalev
tim.lalev4mo ago
Hey Dima, yeah I had weird issues like that too. It's possible to use workarounds :
Tables\Columns\IconColumn::make('created_at')
->label('Has Attachments')
->icon(function ( $record) {
return $record->attachments->count() ? 'heroicon-s-photo' : null;
}),
Tables\Columns\IconColumn::make('created_at')
->label('Has Attachments')
->icon(function ( $record) {
return $record->attachments->count() ? 'heroicon-s-photo' : null;
}),
Notice I intentionally used different column like 'created_at' otherwise if you use 'attachments' it will still render an icon for each count
Dima
Dima4mo ago
Nice, thank you so much
Dennis Koch
Dennis Koch4mo ago
You could also use
->getStateUsing(fn ($record) => $record-
>attachments()->count() > 0)`
->getStateUsing(fn ($record) => $record-
>attachments()->count() > 0)`
or even better, just load the attachments_count instead of loading all the attachments just to check whether they exist.
Want results from more Discord servers?
Add your server