I have three models : Company, User and Role (President, Controller, etc…).
A User can be attached to many Company. - this is my reality. I can have the same RainLab.User account that will be used for two differents companies.
A Company can be attached to many User.
This is a regular Many-To-Many ($belongsToMany in both models) relationship. (company_id | user_id)
Now, I need to introduce the notion of Role and this is how I am not sure how to do this.
company_id | user_id | role_id ?
A user can have many roles such as ‘President’ and ‘Controller’, all specific to the company. The same user can have different roles in differents companies.
Yes a pivot table with role_id is the right way to do it.
Pivot model makes it a bit easier.
use October\Rain\Database\Pivot;
class PersonCompanyPivot extends Pivot
{
public $table = 'xyz_persons_companies';
public $belongsTo = [
'role' => Role::class
];
}
My problem is I need many titles to be attached to Mathieu (in this case for example), for this company. He can be a President but also a Controller.
I tried to change the relation from $belongsTo to $belongsToMany like this :
use October\Rain\Database\Pivot;
class PersonCompanyPivot extends Pivot
{
public $table = 'xyz_persons_companies';
public $belongsTo**Many** = [
'role' => Role::class
];
}
That being said, Laravel is looking for an external table called ‘XYZ.person_company_pivot_title’ of course. I started to try in this direction but didn’t worked that well and I would like to make sure that’s the way to go…