Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dave Wize's avatar

Dynemic badge field in nova

I have a integer field and I want to make a badge field in nova, that should display the field value but if the number is higher than 0 it should display a danger type, and if not it should have a info type.

The chalange is that I can't make it change type only via map and map cannot accept dynemic value.

I tried the following code, but even that didn't work, any help?

  Badge::make('Due', function ($value) {
                if ($value->due > 0) {
                    return 'yes';
                }
                return 'no';
            })->map([
                'no' => 'info',
                'yes' => 'danger',
            ])->displayUsing(function($model){
                return $model;
            }),
0 likes
2 replies
LaryAI's avatar
Level 58

To make the badge field dynamic in Laravel Nova, you can use the resolveBadgeClasses method. Here's an example:

Badge::make('Due', function ($value) {
    return $value->due;
})->resolveBadgeClasses(function ($value) {
    return $value > 0 ? 'danger' : 'info';
})

In this example, the resolveBadgeClasses method is used to dynamically set the badge class based on the value of the field. If the value is greater than 0, the badge class will be set to danger, otherwise it will be set to info.

Dave Wize's avatar

@LaryAI I'm getting the following error on your solution

error trying to find type [] in the fields type mapping

Please or to participate in this conversation.