I would do this as role_id field in your OrganisationUser model, with foreign key to Roles.
Eloquent pivot relationships via another table
Hi
I have User and Organisation models. A single user can belong to many organisations, using a OrganisationUser pivot. A single organisation can have many users. There is an additional field in the pivot for OrgAdmin which allows a user to perform separate actions.
namespace App\Models;
class User extends Authenticatable
{
...
public function organisations()
{
return $this->belongsToMany(Organisation::class)->using(OrganisationUser::class)->withPivot('OrgAdmin');
}
...
}
Organisations have Roles - this is the OrganisationRole model. A Role may be assigned to a User of an Organisation - the OrganisationRole BelongsTo a User. The User may have a different Role per Organisation they are a member of. The same OrganisationRole can belong to many users within the same Organisation. To me, the logical place is to store this association is within the OrganisationUser pivot table shown above.
I am getting out of my depth with Eloquent, is this how you would structure it? What methods would you add into what Models to make this happen, and how would you query said Model so that it would be trivial to do something like:
$user->organisations()->with['OrgAdmin', 'OrganisationRole']->get()
So that I can retrieve something like:
User->name ('Bob'), OrganisationUser->OrgAdmin (1), OrganisationRole->role_name ('bin man')
Thank you in advance
Please or to participate in this conversation.