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

chrisvasey's avatar

Returning relationship of relationship - BelongToThrough?

Hello everyone

hopefully, I can describe this in a way that makes sense - I am having issues researching this as I worry I am missing some kind of understanding of database tables and I was looking to see if someone could point me in the right direction.

I am looking to get the parent of a parent on a model so an example of this would be 3 models that all have belongTo to eachother.

Customer > Site > Task

On the Task.php model I can use:

    public function site()
    {
        return $this->belongsTo('App\Site');
    }

To run $task->site and it will return site.

What I am trying to do is get what customer a task belongs to through the site e.g:

    public function customer()
    {
        return $this->site->customer;
    }

This works as a function but it is not a relationship.

I have looked into using defining custom intermediate tables (pivot tables) and also looked at doing joins and through relationships but I worry I might be looking at this issue in the wrong way.

I need this relationship exposed to display in a Nova table to I would much rather use a true relationship rather than a function - it is something I seem to hit in most projects so wanted to get my understanding checked!

How would others approach this?

Thanks for your help

0 likes
3 replies
chrisvasey's avatar

I had actually seen this (as hinted in the title) but was worried there was a more native way to do this. I implemented the package and it has worked a charm:

Task.php


public function customer()
{
    return $this->belongsToThrough('App\Customer', 'App\Site');
}

Nova/Task.php

BelongsTo::make('Customer', 'customer', 'App\Nova\Customer');

Turns out it still treats it as a normal belongsTo relationship so everything works fine.

I would still love to know if there is a way to do this with joins etc but for now I am happy with this, thanks for your help!

mabdullahsari's avatar

This package is handling those joins for you behind the scenes. I would personally not bother implementing it myself as this package works flawlessly.

Please or to participate in this conversation.