F
Filamentβ€’2y ago
_wbit

BadgeColumn -> condition using values from two columns

I'm trying to use BadgeColumn on a table with 'stock' and 'stock_alert' columns and my intention is to paint the 'stock' with a color based on the relationship to the 'stock_alert', my code is as follows: BadgeColumn::make('stock') ->label('Stocks') ->colors([ 'success', 'danger' => static fn ($stock): bool => $stock === 0, 'primary' => static fn ($stock) => $stock <= DB::select('stock_alert'), ]), The 'danger' color applies fine when $stock is 0. But the problem is in comparing $stock with 'stock_alert', I can't get that value.
2 Replies
Patrick Boivin
Patrick Boivinβ€’2y ago
Lol, just noticed your post here. I'll paste the answer we discussed in case it can be useful to others. You can access the $record variable through the ->color() method :
->color(function ($record): string {
if ($record->stock === 0) {
return 'danger';
}

if ($record->stock > $record->stock_alert) {
return 'primary';
}

return 'success';
})
->color(function ($record): string {
if ($record->stock === 0) {
return 'danger';
}

if ($record->stock > $record->stock_alert) {
return 'primary';
}

return 'success';
})
_wbit
_wbitOPβ€’2y ago
Thanks again @pboivin πŸ‘πŸ½
Want results from more Discord servers?
Add your server