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
BlackShadow
BlackShadow4mo ago
$record->yourRelation->yourColumn == 'something' ??
Schilly
Schilly4mo ago
Tried that does not register it existing 😐 I get a "Property [is_primary] does not exist on this collection instance." with code:
->color(
function($record) {
if($record->squadrons->is_primary == 1) {
return 'success';
}
}
),
->color(
function($record) {
if($record->squadrons->is_primary == 1) {
return 'success';
}
}
),
squadrons being the relationship that has the ->withPivot('is_primary') on it. Here is the full text Column
Tables\Columns\TextColumn::make('squadrons.abbrv')
->label('Squadron(s)')
->badge()
->color(

function($record) {
dd($record->squadrons);
if($record->squadrons->is_primary == 1) {
return 'success';
}
}
),
Tables\Columns\TextColumn::make('squadrons.abbrv')
->label('Squadron(s)')
->badge()
->color(

function($record) {
dd($record->squadrons);
if($record->squadrons->is_primary == 1) {
return 'success';
}
}
),
BlackShadow
BlackShadow4mo ago
Because it returns a collection of "squadrons" You could get the first ->first()->is_primary But not sure if thats waht you want.
Schilly
Schilly4mo ago
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
BlackShadow
BlackShadow4mo ago
Where does it work
Schilly
Schilly4mo ago
In the edit the relaionship of the member
BlackShadow
BlackShadow4mo ago
Show
Schilly
Schilly4mo ago
in the SquadronRelationshipManager - I am able to set a toggle column of it
Tables\Columns\ToggleColumn::make('is_primary')
->label('Primary Squadron')
->alignCenter()
->onIcon('heroicon-s-check-circle')
->offIcon('heroicon-o-check-circle')
->onColor('success')
->beforeStateUpdated(function ($record, $state) {
$usrSqdns = User::find($record->user_id)->squadrons;

$pivots = array();
foreach($usrSqdns as $sqn)
{
$pivots[] = $sqn->id;
}

User::find($record->user_id)->squadrons()->syncWithPivotValues($pivots,['is_primary' => 0]);
})
Tables\Columns\ToggleColumn::make('is_primary')
->label('Primary Squadron')
->alignCenter()
->onIcon('heroicon-s-check-circle')
->offIcon('heroicon-o-check-circle')
->onColor('success')
->beforeStateUpdated(function ($record, $state) {
$usrSqdns = User::find($record->user_id)->squadrons;

$pivots = array();
foreach($usrSqdns as $sqn)
{
$pivots[] = $sqn->id;
}

User::find($record->user_id)->squadrons()->syncWithPivotValues($pivots,['is_primary' => 0]);
})
BlackShadow
BlackShadow4mo ago
Like i said, squadrons return a list of items. Right?
Schilly
Schilly4mo ago
Correct
BlackShadow
BlackShadow4mo ago
So you can't just call 'is_primary'
Schilly
Schilly4mo ago
Which makes sense
BlackShadow
BlackShadow4mo ago
Perhaps you can do squadrons()->firstWhere('is_primary')
Schilly
Schilly4mo ago
Well that doesn't throw an error - also doesnt do anything lol $record->squadrons()->firstWhere('is_primary') just returns null
BlackShadow
BlackShadow4mo ago
$record->squadrons()->firstWhere('is_primary', true)
$record->squadrons()->firstWhere('is_primary', true)
Wait What is squadrons.abbrv
Schilly
Schilly4mo ago
a column on the squadrons table not on the pivot So pulling the relationship squadrons, displaying the abbrv column in the table
Schilly
Schilly4mo ago
which produces:
No description
BlackShadow
BlackShadow4mo ago
I see
Schilly
Schilly4mo ago
could change it to squadrons.id then search from there or something?
BlackShadow
BlackShadow4mo ago
one second I think you need to get the state 😛
BlackShadow
BlackShadow4mo ago
Perhaps something like this;
Tables\Columns\TextColumn::make('squadrons.id')
->label('Squadron(s)')
->badge()
->color(function(string $state, $record) {
$findSquadron = $record->squadrons->where('id', $state)->exists() // or ->first()
if($findSquadron->is_primary) {
return 'success';
}
return 'info';
}
),
Tables\Columns\TextColumn::make('squadrons.id')
->label('Squadron(s)')
->badge()
->color(function(string $state, $record) {
$findSquadron = $record->squadrons->where('id', $state)->exists() // or ->first()
if($findSquadron->is_primary) {
return 'success';
}
return 'info';
}
),
Not tested ofc.
Schilly
Schilly4mo ago
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 does
BlackShadow
BlackShadow4mo ago
Yea What does $state return
Schilly
Schilly4mo ago
"9"
BlackShadow
BlackShadow4mo ago
What does findSquadron return?
Schilly
Schilly4mo ago
null
Schilly
Schilly4mo ago
I feel as if so given there couple be multiple returns - there should be a foreach in here somewhere ie foreach$record ... thoughts?
BlackShadow
BlackShadow4mo ago
Perhaps
Schilly
Schilly4mo ago
or rather `foreach($record->squadrons as ...)
BlackShadow
BlackShadow4mo ago
I have never used badges like this Only for 1 status.
Schilly
Schilly4mo ago
Yea This is def. an abnormal use case lol
BlackShadow
BlackShadow4mo ago
Maybe not supported Maybe a custom column?
Schilly
Schilly4mo ago
Maybe
BlackShadow
BlackShadow4mo ago
Take a read here, maybe its usefull
Schilly
Schilly4mo ago
Im just simply trying to identify which of the returned instances of squadron is primary loll
BlackShadow
BlackShadow4mo ago
Hope someone else comes along and knows the answer. 🤷‍♂️
Schilly
Schilly4mo ago
Me too lol Halp! Im Lost! lol Thanks for tryin tho @CodeWithDennis much appreciated AHAH! figured it out!
BlackShadow
BlackShadow4mo ago
Now im curious
Schilly
Schilly4mo ago
Tables\Columns\TextColumn::make('squadrons.id')
->label('Squadron(s)')
->formatStateUsing(fn (string $state, $record): string => $record->squadrons->find($state)->abbrv)
->badge()
->color(
function(string $state, $record) {
$target = $record->squadrons->find($state);

if($target->pivot->is_primary == '1') {
return 'success';
}
}
),
Tables\Columns\TextColumn::make('squadrons.id')
->label('Squadron(s)')
->formatStateUsing(fn (string $state, $record): string => $record->squadrons->find($state)->abbrv)
->badge()
->color(
function(string $state, $record) {
$target = $record->squadrons->find($state);

if($target->pivot->is_primary == '1') {
return 'success';
}
}
),
BlackShadow
BlackShadow4mo ago
Wait, thats what we did before. ^ Ah we didn't use ->pivot
Schilly
Schilly4mo ago
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:
Schilly
Schilly4mo ago
No description
BlackShadow
BlackShadow4mo ago
if($record->squadrons->find($state)?->pivot->is_primary) {
return 'success';
}
if($record->squadrons->find($state)?->pivot->is_primary) {
return 'success';
}
Does this work 😛 ?
Schilly
Schilly4mo ago
Yes much cleaner lol
BlackShadow
BlackShadow4mo ago
👌
Schilly
Schilly4mo ago
I need to work on my code refactoring thats for sure lol
BlackShadow
BlackShadow4mo ago
Glad it is fixed 🙂
Schilly
Schilly4mo ago
Same. Thanks man much appreciated 😄 Now if only the badges were ordered to have the primary show up first... 😛
Schilly
Schilly4mo ago
Lol! thanks
->orderBy('is_primary','desc')
->orderBy('is_primary','desc')
on the relationship(s) easy peasy 😛
BlackShadow
BlackShadow4mo ago
Nice