php - laravel: add constraint on many-to-many relationships -
consider simple many-to-many relationship in laravel:
class user extends eloquent { public function roles() { homecoming $this->belongstomany('role'); } }
and class role extends eloquent {
public function users() { homecoming $this->belongstomany('user'); } }
this ok schema of role_user
keeps user_id
, role_id
. if have other constraints? illustration in app github, user admin in repository , regular user in repository b. add together repository_id
role_user
table, when want query user->roles
want in current repository, that maintain reference of current_repository_id
in session . changes should in models that?
note: don't want alter in using-code! isn't anyway set filter logic in model declaration? because i'm @ middle of big project , it's tough alter every usage. possible?
//user model public function roles() { $repoid = myapp::currentrepoid(); homecoming $this->belongstomany('role', 'pivot_table_name', 'user_id', 'role_id') ->wherepivot('repository_id', $repoid); }
php laravel laravel-4 eloquent
No comments:
Post a Comment