Quite simple,
public function users()
{
return $this->hasManyThrough(
'App\User',
'App\UsersOrganization',
'organization_cmpwwn',
'cmp_wwn',
'cmp_wwn',
'user_cmpwwn'
);
}
You can access any other rolecodes like so: $organisation->user()->select(['role_code'])->where('role_code', ContactOrg).
If you have a limited number of role_codes you can create a method for each one fo them in your Organisation Model like so:
public function contactOrgUsers()
{
return $this->users()->select(['role_code'])->where('role_code', 'ContactOrg')->get();
}
and you can acces them this way $organisation->contactOrgUsers().
In case you have unknown number of role_codes you can use arguments:
public function usersWithRole(String $roleCode)
{
return $this->users()->select(['role_code'])->where('role_code', $roleCode)->get();
}
Hope this helps. :)