How to load multilevel relationships?

2

Hello, I'm trying to search 3 related tables as follows contracts belongsTo - > companies hasMany - > company_addresses. I would like to display company_addresses data in the contracts view.

contracts
    id – integer
    company_id - integer
    title – string 

companies
    id - integer
    name – string

company_addresses
    id - integer
    company_id - integer
    address – string
    complement – string
    contact– string

Model contracts

public function company()
{
    return $this->belongsTo(Company::class, 'company_id', 'id');
}

Model companies

public function contracts ()
{
    return $this->hasMany(Contract::class);
} 
public function addresses()
{
    return $this->hasMany(CompanyAddress::class);
}

Model company_addresses

public function company()
{
    return $this->belongsTo(Company::class);
}

Contract Controller Code

 public function index()
 {
     $contract= Contract::with('companies.company_addresses')->get();
     return view(‘contract.view')->with(compact('contract'));
 }

So, what am I doing wrong and how can I fix it? Any help is very valid.

Thank you in advance.

    
asked by anonymous 18.04.2018 / 18:38

0 answers