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 a partition matric for a belongs to relationship?

I have a nova partition matric that I want to display a list of events from an event model, and I want to group by with event_type_id column which is a velongsTo relationship.

The problem is that I want to get the name of the relatioship on it and not the id which is stored on the column event_type_id. Is this possible?

0 likes
2 replies
LaryAI's avatar
Level 58

Yes, it is possible to display the name of the relationship instead of the id in the partition matric. You can use the Laravel's Eloquent ORM's relationship methods to achieve this.

Assuming you have a "EventType" model for the "event_type" table, you can define a "belongsTo" relationship in your "Event" model like this:

class Event extends Model
{
    public function eventType()
    {
        return $this->belongsTo(EventType::class);
    }
}

Then, in your Nova resource file, you can define the partition matric like this:

Partition::make('Events by Type', 'event_type_id')
    ->sortDescending()
    ->label(function ($value) {
        return EventType::find($value)->name;
    })

Here, we are using the "label" method to display the name of the related "EventType" model instead of the id. The "label" method takes a closure that receives the value of the partition and returns the label to be displayed.

Note that this approach will result in additional database queries for each partition label. If you have a large number of partitions, you may want to consider using eager loading to reduce the number of queries.

Dave Wize's avatar

@LaryAI This is wrong code.

Partition::make('Events by Type', 'event_type_id')
    ->sortDescending()
    ->label(function ($value) {
        return EventType::find($value)->name;
    })

It is not valid....

Any human reponse please ?

Please or to participate in this conversation.