Laravel两个很多相关表之间雄辩的关系

时间:2015-01-09 07:16:25

标签: laravel

我有很多相关的两个表,“角色”和“用户”,数据透视表为“user_roles”。

在角色模型中

$ this-> belongsToMany('users','user_roles');

在用户模型中

$ this-> belongsToMany('roles','user_roles');

这是一种有效的关系吗?

2 个答案:

答案 0 :(得分:0)

您需要使用数据透视表和多对多关系,使用您所说的数据透视表。

Laravel documentation - Eloquent

中解释了您要做的事情

答案 1 :(得分:0)

没有。第一个参数应该是相关模型的类名:

$this->belongsToMany('User', 'user_roles');

$this->belongsToMany('Role', 'user_roles');

否则,如果您的外键符合惯例,您应该没事......

See the documentation for more info