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

SarahS's avatar
Level 12

Referencing a 'grandfather' table

I'm not sure what the right term is for this but I have three tables:

  • Clients - id|name
  • Types - id|client_id|type|name
  • IndividualProfiles - id|type_id|profiledata

Clients is one-to-many on Types Types is one-to-many on IndividualProfiles.

So an IndividualProfile is ultimately only associated with one Client.

Before the Types table didn't exist and this is what was in my IndividualProfilesController:

    public function index()
    {
        $individualprofiles = IndividualProfile::where('client_id', session('client_id'))->get();
        
        return view('individualprofiles.index', ['individualprofiles' => $individualprofiles]);
    }

But now I have to use type_id because there is no client_id on the Individual Table. So, my question is, is there an easy way of getting that client_id in Laravel? Or, should I be adding the client_id to my IndividualProfiles table?

0 likes
3 replies
SarahS's avatar
Level 12

Thank you, I will check that out and see if I can apply it.

SarahS's avatar
Level 12

OK, so I've added this to my IndividualProfile Model:

    /**
     * Get the client.
     */
    public function typeClient()
    {
        return $this->hasOneThrough('App\Client', 'App\Type');
    }

How do I now get access to it? The code here still doesn't work:

    public function index()
    {
        $individualprofiles = IndividualProfile::where('client_id', session('client_id'))->get();
        
        return view('individualprofiles.index', ['individualprofiles' => $individualprofiles]);
    }

Please or to participate in this conversation.