jeudi 5 septembre 2019

How to retrieve intermediate table data while using hasManyThrough?

My database structure

Company:

  • id;
  • other_fields;

staff:

  • id;
  • company_id;
  • user_id;
  • staff_type;

user:

  • id;
  • other_fields;

Requirement:

1st: I want to access company data with User_Id & Users data with company_id.

2nd: I also want staff_type data to be available when user data is retrieved using company_id with hasManyThrough.

I succeed at first requirement with hasManyThrough()

    public function user()
    {
        return $this->hasManyThrough(
            'App\User',  // model of required table
            'App\models\Staff',  // model of intermediate table
            'company_id', // fk of intermediate table from this model (company)
            'id', // Foreign key OR Key of required table
            'id', // Key of this model (company table)
            'user_id' // Local key or Key of required table (User) in intermediate table (staff table)
        );
    }

I couldn't get intermediate table (staff table) data (staf_type). How to get it?

I expect solution use eloquent at best.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire