Custom Icon Column

Trying to figure out what I am doing wrong here, I am viewing a User and want to show a checkmark on the user resource if they have a gallery item and they have verified their email. It is making the column but the column is blank.
Tables\Columns\IconColumn::make('Onboarded')
->icon(function ($record) {
return $record->profile->hasMedia('gallery')
&& $record->email_verified_at !== NULL
? 'heroicon-o-clock'
: 'heroicon-o-check-circle';
}),
Tables\Columns\IconColumn::make('Onboarded')
->icon(function ($record) {
return $record->profile->hasMedia('gallery')
&& $record->email_verified_at !== NULL
? 'heroicon-o-clock'
: 'heroicon-o-check-circle';
}),
No description
5 Replies
awcodes
awcodes11mo ago
You don’t have an Onboard field on the record, so it’s retuning null which will cause the column to not render. So you need to use ->getStateUsing() to return a non null value.
datarecall
datarecallOP11mo ago
did a quick search for getStateUsing in docs and text column but can't find any examples
awcodes
awcodes11mo ago
Sorry, try ->default() instead.
datarecall
datarecallOP11mo ago
then inside ->default use the record to check the relationship ?
Tables\Columns\IconColumn::make('Onboarded')
->default(function ($record) {
return $record->profile->hasMedia('gallery');
})
->icon(function ($state) {
return $state
? 'heroicon-o-clock'
: 'heroicon-o-check-circle';
}),
Tables\Columns\IconColumn::make('Onboarded')
->default(function ($record) {
return $record->profile->hasMedia('gallery');
})
->icon(function ($state) {
return $state
? 'heroicon-o-clock'
: 'heroicon-o-check-circle';
}),
awcodes
awcodes11mo ago
icon() should be an array where the key is the state. So, this case, it’d be
->icon([
true => ‘heroicon-o-check-circle’,
false => ‘heroicon-o-clock’
])
->icon([
true => ‘heroicon-o-check-circle’,
false => ‘heroicon-o-clock’
])
Icon will automatically read the state so no need to use a callback for it. In this particular case though an attribute on the model might be easier unless you need to filter on the value.

Did you find this page helpful?