Consider this simple many-to-many relationship in Laravel:
class User extends Eloquent {
public function roles()
{
return $this->belongsToMany('Role');
}
}
and
class Role extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
This is ok with schema of role_user
that keeps user_id
and role_id
.
But what we if have some other constraints? For example in an app like Github, user is admin in repository A and is regular user in repository B. So we add repository_id
to role_user
table, but when I want to query user->roles
I want to look for that in current repository, that I keep reference of current_repository_id
in session. What changes should I do in models to do that?
Note: I don't want to change anything in using-code! Isn't anyway to put filter logic in model declaration? Because I'm at the middle of big project and it's tough to change every usage. Is it possible?