In my application, I need the tables users and companies, which will have a one-to-many relationship where the user hasMany companies. Now I'll need to add roles here where a user can have multiple roles inside a company.
- User1 will have role1, role2 in company1
- User1 will have role2, role3 in company2
- etc.
To do that, the new table I was thinking of will have three rows: id, user_id, role_id, and company_id
. Can someone help me to make the relationships between the models in Laravel Eloquent? So, that in the end, I should be able to get something like the following.
- To get what roles user1 has on company1 -> $user1->company1->roles
- To get what roles user1 has on company2 -> $user1->company2->roles