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
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 :
Thanks again @pboivin ππ½