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

Romain's avatar
Level 30

Can I have a field with a relation through another relation?

Hey there,

quite simple I think, is there a simple way to have access to a relationship through another one?

I have Show <-> Season <-> Episode

on each episode I want to show the Season and the Show name. For now I figured how to display it by doing:

public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),
            BelongsTo::make('Season'),
            Text::make('Show', function ()
            {
                return $this->season->show->name;
            }),
            
        ];
    }

It works fine, but I don't get a link to the show, I would have to write it myself. Is there a way to have a link between episode and show without adding show_id on each episode?

thanks

0 likes
5 replies
Romain's avatar
Level 30

Sure, but then in Nova, what do I use? HasMany?

Romain's avatar
Level 30

Well I managed to do what I wanted with your suggestion: In nova:

public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),
            BelongsTo::make('Season', 'name', Season::class),
            Text::make('Show', function ()
            {
                return $this->show->name; << No need for season anymore
            }),
            HasOne::make('Show')
        ];
    }

and in the Models\Episode

public function show()
    {
        return $this->hasOneThrough(Show::class, Season::class, 'id', 'id', 'season_id', 'show_id');
    }

I just needed to invert the IDs in the hasOneThrough to make it work.

ismaile's avatar

Perfect, I'm glad to see that you could find the right solution.

Please or to participate in this conversation.