I am trying to implement a feature and I don't think it's available by relationships.
A User may have many Companies and Company may have many Users. They are connected via company_employees pivot table.
pivot table looks like this:
company_id, user_id, position_id
I want to have an option to fetch company employees with their assosiated position.
Let's say there are companies:
- "Foo Company"
- "Bar Company"
and user
- "John Doe"
There are also 2 positions:
- "Frontend Developer"
- "Backend Developer"
John Doe is an employee in both of these companies with different position.
company_id | user_id | position_id
1 1 1
2 1 2
How can i make something like
$company = Company::first();
$company->employees()->with('position')->get();
return only position associated with first user that belongs to first company as HasOne relationship?
Companymodel and then tryCompany::with(['pivot_table.user','pivot_table.position'])->first();