How to do IconColumn with reverse boolean?

Usually when I want do do an IconColumn with boolean i just do this
IconColumn::make('is_human_at')
->boolean()
IconColumn::make('is_human_at')
->boolean()
However i have a column that need the reverse so I tried to do this
IconColumn::make('is_human_at')
->label('is_robot')
->icons([
'heroicon-o-check-circle' => fn (?string $state): bool => $state !== null,
'heroicon-o-x-circle' => fn (?string $state): bool => $state !== null,
])
->colors([
'success' => fn (?string $state): bool => $state !== null,
'danger' => fn (?string $state): bool => $state !== null,
]),
IconColumn::make('is_human_at')
->label('is_robot')
->icons([
'heroicon-o-check-circle' => fn (?string $state): bool => $state !== null,
'heroicon-o-x-circle' => fn (?string $state): bool => $state !== null,
])
->colors([
'success' => fn (?string $state): bool => $state !== null,
'danger' => fn (?string $state): bool => $state !== null,
]),
However for the 2nd one, only the danger red shows, when it is supposed to be green it does not show at all
9 Replies
pocket.racer
pocket.racerOP15mo ago
as u can see, only the red one shows, not the blue one. I can't just use ->boolean() because what i need is the inverse
No description
Vp
Vp15mo ago
Because your condition is same, it check first line and check the second line, the condition is same that's why it shows heroicon-o-x-circle and danger only Try like this
->icons([
'heroicon-o-check-circle' => fn (?string $state): bool => $state == null,
'heroicon-o-x-circle' => fn (?string $state): bool => $state !== null,
])
->colors([
'success' => fn (?string $state): bool => $state == null,
'danger' => fn (?string $state): bool => $state !== null,
]),
->icons([
'heroicon-o-check-circle' => fn (?string $state): bool => $state == null,
'heroicon-o-x-circle' => fn (?string $state): bool => $state !== null,
])
->colors([
'success' => fn (?string $state): bool => $state == null,
'danger' => fn (?string $state): bool => $state !== null,
]),
Using this condition, check-circle with success will come if it's null, otherwise x-circle with danger
pocket.racer
pocket.racerOP15mo ago
my bad my code earlier have a mistake I changed to this but it still don't show green, but when is red (danger) it show
IconColumn::make('is_human_at')
->label('Is Robot')
->icons([
'heroicon-o-check-circle' => fn (?string $state): bool => $state === null,
'heroicon-o-x-circle' => fn (?string $state): bool => $state !== null,
])
->colors([
'success' => fn (?string $state): bool => $state === null,
'danger' => fn (?string $state): bool => $state !== null,
]),
IconColumn::make('is_human_at')
->label('Is Robot')
->icons([
'heroicon-o-check-circle' => fn (?string $state): bool => $state === null,
'heroicon-o-x-circle' => fn (?string $state): bool => $state !== null,
])
->colors([
'success' => fn (?string $state): bool => $state === null,
'danger' => fn (?string $state): bool => $state !== null,
]),
Vp
Vp15mo ago
Your is_human_at is boolean column right, then did you casts in model
pocket.racer
pocket.racerOP15mo ago
is_human_at is a nullable datetime column null is false have value is true But my requirement is to do is robot so i need to reverse it instead of do ->boolean() if there is an ->inverseBoolean() method that will make things easier maybe i can have a fake model attribute called is_robot too that inverse is_human_at behind the scene but i would need to think of ensuring sorting still work
Vp
Vp15mo ago
so, if null you want red color, if not null green color
pocket.racer
pocket.racerOP15mo ago
my example sound weird bcos i give hypothetical example But in the actual code base the column is employment_pass if employment_pass have value, this person is a foreigner if employment_pass is null, this person is a Citizen When displaying to the users The displayed label is Citizen, but because the underlying database column is employment_pass, i need to inverse it so null i want green colour check (Citizen) not null i want red colour X (Non Citizen because employment_pass have a value) hope the real example makes more sense 😅 if it is not the inverse then ->boolean() would be the easy way i guess i can create a dynamic attribute too using Eloquent accessor method but if i do that i need sorting to still work too
Vp
Vp15mo ago
I would suggest using boolean column, I check the code and I cannot get anything when value is null in IconColumn, but you can use placeholder() also when empty
cheesegrits
cheesegrits15mo ago
Check this question, and adapt the answer to suit. https://discord.com/channels/883083792112300104/1156331989155188926 Point being, you can use a column name that doesn't exist as a field on the table, and use getStateUsing() to handle the boolean logic. You wouldn't have to set the icons, just return something like is_null($record->employment_pass) from the getStateUsing() closure. Sry, on my phone so I can't write full code for you.
Want results from more Discord servers?
Add your server