I have 2 models with a relationship Company and DamageReport.
A DamageReport is always linked to a Company by the key company_id.
So company_id in DamageReport equals id in Company.
Very simple, right? Now my goal is to query the Company when I know the id of the DamageReport.
For example
I have a row of the DamageReport table:
id company_id
6 1
And the record of Company with id is:
id name
1 Company 1
So in my controller I have the DamageReport id (6) and need to query company with id 1.
I've set up a relationship like this in my models
Company model:
/**
* The Damage Reprots that belong to the Company.
*/
public function damageReports()
{
return $this->belongsToMany('App\DamageReport');
}
DamageReport model:
/**
* The company of a damagereport
*
*/
public function company()
{
return $this->belongsTo('App\Company');
}
Now in my controller I tried something like this but I honestly have no clue
$company = new Company;
$company = $company->company($damageReportId);
dd($company);