baguus
2 years ago

Belongstomany, hasMany relationship back on same table

Posted 2 years ago by baguus

Hi guys I'm trying to build relationships between companies and company_budgets table. In company table I have an id and in company_budgets I have id, company_id, sponsor_id and budget. What I want are to acheive is: One company can sponsor multiple companies in the same table and one company can be sponsored by multiple companies. How should I create the relationships?

My failed attempts:

public function sponsors() {
return $this->belongsToMany('App\Company','company_sponsors','company_id','sponsor_id')->withPivot('budget');
}

public function companies() {
return $this->hasMany('App\Company','company_sponsors','sponsor_id','company_id')->withPivot('budget');
}

And the next question would be, how can I query the pivot table?

Please sign in or create an account to participate in this conversation.