How to do IconColumn with reverse boolean?
Usually when I want do do an IconColumn with boolean i just do this
However i have a column that need the reverse so I tried to do this
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
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
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
Using this condition, check-circle
with success
will come if it's null, otherwise x-circle
with danger
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
Your
is_human_at
is boolean column right, then did you casts in modelis_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
so, if null you want red color, if not null green color
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
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
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.