F
Filament10mo ago
Schilly

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
BlackShadow10mo ago
$record->yourRelation->yourColumn == 'something' ??
Schilly
SchillyOP10mo 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
BlackShadow10mo 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
SchillyOP10mo 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
BlackShadow10mo ago
Where does it work
Schilly
SchillyOP10mo ago
In the edit the relaionship of the member
BlackShadow
BlackShadow10mo ago
Show
Schilly
SchillyOP10mo 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
BlackShadow10mo ago
Like i said, squadrons return a list of items. Right?
Schilly
SchillyOP10mo ago
Correct
BlackShadow
BlackShadow10mo ago
So you can't just call 'is_primary'
Schilly
SchillyOP10mo ago
Which makes sense
BlackShadow
BlackShadow10mo ago
Perhaps you can do squadrons()->firstWhere('is_primary')
Schilly
SchillyOP10mo ago
Well that doesn't throw an error - also doesnt do anything lol $record->squadrons()->firstWhere('is_primary') just returns null
BlackShadow
BlackShadow10mo ago
$record->squadrons()->firstWhere('is_primary', true)
$record->squadrons()->firstWhere('is_primary', true)
Wait What is squadrons.abbrv
Schilly
SchillyOP10mo ago
a column on the squadrons table not on the pivot So pulling the relationship squadrons, displaying the abbrv column in the table
Schilly
SchillyOP10mo ago
which produces:
No description
BlackShadow
BlackShadow10mo ago
I see
Schilly
SchillyOP10mo ago
could change it to squadrons.id then search from there or something?
BlackShadow
BlackShadow10mo ago
one second I think you need to get the state 😛
BlackShadow
BlackShadow10mo 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
SchillyOP10mo 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
BlackShadow10mo ago
Yea What does $state return
Schilly
SchillyOP10mo ago
"9"
BlackShadow
BlackShadow10mo ago
What does findSquadron return?
Schilly
SchillyOP10mo ago
null
Schilly
SchillyOP10mo 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
BlackShadow10mo ago
Perhaps
Schilly
SchillyOP10mo ago
or rather `foreach($record->squadrons as ...)
BlackShadow
BlackShadow10mo ago
I have never used badges like this Only for 1 status.
Schilly
SchillyOP10mo ago
Yea This is def. an abnormal use case lol
BlackShadow
BlackShadow10mo ago
Maybe not supported Maybe a custom column?
Schilly
SchillyOP10mo ago
Maybe
BlackShadow
BlackShadow10mo ago
Take a read here, maybe its usefull
Schilly
SchillyOP10mo ago
Im just simply trying to identify which of the returned instances of squadron is primary loll
BlackShadow
BlackShadow10mo ago
Hope someone else comes along and knows the answer. 🤷‍♂️
Schilly
SchillyOP10mo ago
Me too lol Halp! Im Lost! lol Thanks for tryin tho @CodeWithDennis much appreciated AHAH! figured it out!
BlackShadow
BlackShadow10mo ago
Now im curious
Schilly
SchillyOP10mo 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
BlackShadow10mo ago
Wait, thats what we did before. ^ Ah we didn't use ->pivot
Schilly
SchillyOP10mo 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
SchillyOP10mo ago
No description
BlackShadow
BlackShadow10mo 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
SchillyOP10mo ago
Yes much cleaner lol
BlackShadow
BlackShadow10mo ago
👌
Schilly
SchillyOP10mo ago
I need to work on my code refactoring thats for sure lol
BlackShadow
BlackShadow10mo ago
Glad it is fixed 🙂
Schilly
SchillyOP10mo ago
Same. Thanks man much appreciated 😄 Now if only the badges were ordered to have the primary show up first... 😛
Schilly
SchillyOP10mo ago
Lol! thanks
->orderBy('is_primary','desc')
->orderBy('is_primary','desc')
on the relationship(s) easy peasy 😛
BlackShadow
BlackShadow10mo ago
Nice
Want results from more Discord servers?
Add your server