Text Column Badge Color based on Pivot attribute
Hello,
I am trying to define a badge color based on the value of a pivot attribute.
For example:
- User
belongsToMany
Squadrons and the Squadrons belongsToMany
Users - both withPivot('is_primary')
.
I am hoping to change the badge color based on the Boolean is_primary
- I cannot for the life of me figure out how to get it to dynamically change based on the pivot Boolean.
Thoughts?54 Replies
$record->yourRelation->yourColumn == 'something'
??Tried that
does not register it existing 😐
I get a "Property [is_primary] does not exist on this collection instance."
with code:
squadrons being the relationship that has the
->withPivot('is_primary')
on it.
Here is the full text Column
Because it returns a collection of "squadrons"
You could get the first
->first()->is_primary
But not sure if thats waht you want.No I have a flag on the pivot that is "is_primary"
which that part works fine elsewhere - just wanting to colorize just the primary flagged relationship a differnet color
Where does it work
In the edit the relaionship of the member
Show
in the SquadronRelationshipManager - I am able to set a toggle column of it
Like i said,
squadrons
return a list of items.
Right?Correct
So you can't just call 'is_primary'
Which makes sense
Perhaps you can do
squadrons()->firstWhere('is_primary')
Well that doesn't throw an error - also doesnt do anything lol
$record->squadrons()->firstWhere('is_primary') just returns null
Wait
What is
squadrons.abbrv
a column on the squadrons table
not on the pivot
So pulling the relationship squadrons, displaying the
abbrv
column
in the tablewhich produces:
I see
could change it to squadrons.id
then search from there or something?
one second
I think you need to get the state 😛
Perhaps something like this;
Not tested ofc.
I was thinking that too
But that just returns them all as
info
(side note ->exists() does not exist on the collection apparently)
but first doesYea
What does
$state
return"9"
What does findSquadron return?
null
I feel as if so given there couple be multiple returns - there should be a foreach in here somewhere
ie foreach$record ...
thoughts?
Perhaps
or rather `foreach($record->squadrons as ...)
I have never used badges like this
Only for 1 status.
Yea
This is def. an abnormal use case lol
Maybe not supported
Maybe a custom column?
Maybe
Take a read here, maybe its usefull
Im just simply trying to identify which of the returned instances of
squadron
is primary lollHope someone else comes along and knows the answer. 🤷♂️
Me too lol
Halp! Im Lost! lol
Thanks for tryin tho @CodeWithDennis much appreciated
AHAH!
figured it out!
Now im curious
Wait, thats what we did before.
^
Ah we didn't use ->pivot
yea
and also used a
where
isntead of find()
not that should make a difference
as find is just a where on only the ID column
But now i get:
Does this work 😛 ?
Yes
much cleaner lol
👌
I need to work on my code refactoring thats for sure lol
Glad it is fixed 🙂
Same. Thanks man much appreciated 😄
Now if only the badges were ordered to have the primary show up first... 😛
Lol! thanks
on the relationship(s)
easy peasy 😛
Nice