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

Dave Wize's avatar

Can I add relationship names into a partition matric?

I have a partition matric class with the folwing code.

class EventType extends Partition
{
    /**
     * Calculate the value of the metric.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return mixed
     */
    public function calculate(NovaRequest $request)
    {
        return $this->count($request, Event::class, 'event_type_id');
    }

I want to make the labels, show the name off the event_type and not the id number.

The official documentiotion says we need to do something like this

 ->label(fn ($value) => match ($value) {
                null => 'None',
                default => ucfirst($value)
            });

I tried working with this but I was unable to get it to work. I did something like this

->label(function ($value) {
        return EventType::find($value)->name;
    })

But it returned an error,

cannot read property name on NULL

Any help on this issue?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message suggests that the find method is returning null, which means that the $value argument passed to the label function is not a valid event type ID. To avoid this error, you can add a check to make sure that the $value argument is not null before calling the find method. Here's an updated version of the label function:

->label(function ($value) {
    if ($value === null) {
        return 'None';
    }
    $eventType = EventType::find($value);
    return $eventType ? $eventType->name : 'Unknown';
})

This code first checks if $value is null, and returns the string 'None' if it is. Otherwise, it tries to find the event type with the given ID using the find method. If the event type is found, it returns its name. If not, it returns the string 'Unknown'.

Please or to participate in this conversation.