I just started working on a new project and I have a question about pivot tables.
I have three tables:
- users
- roles
- companies
A user can have multiple roles and a role can have multiple users (obviously). Thats working fine with a pivot table role_user.
A user can also be part of multiple companies, but the role in each company can be different.
For example:
John is 'owner' and 'member' of company A
John is 'member' of Company B
My question, how can I program that in a good Laravel way?
It should be possible to have function like $user->hasRoleInCompany('member', 'A') to check if a user has a certain role in a certain company.
I searched high and low and did not find a real answer.
I did get some ok results altering a pivot table to take three id columns (one for each of the tables) and then using laravel standard attach functionality to update two of these ids, and adding the third id using attach's optional parameter(s).
Is there anyway you could make it easier to understand your database relationships?
But from what I read, you'll need "hasManyThrough()" and "belongsToMany()". Just put them in each model and link whatever you need to via the pivot tables/functions