我正在为将身份验证与公司的LDAP服务器耦合在一起的软件构建数据库。我现在有两个表
AD_Groups
和
AD_Users
在表中被连接的人
AD_UsersXAD_Groups
我已经了解了如何雄辩地建立关系。官方文档中通过以下示例说明了多对多关系:https://laravel.com/docs/5.8/eloquent-relationships#many-to-many
现在,如您所见,口才的以下功能对我没有多大帮助:
"To define this relationship, three database tables are needed: users, roles, and role_user. The role_user table is derived from the alphabetical order of the related model names, and contains the user_id and role_id columns."
因此,我需要使用第二个参数来覆盖此派生名称,如此处所述:
"As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method:
return $this->belongsToMany('App\Role', 'role_user');
但是从文档的以上示例中可以看出,臭名昭著的“蛇案”仍然适用于该名称。但是,我认为这可能不适用于我的情况。诚然,AD_UsersXAD_Groups
非常丑陋,我担心雄辩/流明将无法正确识别其元素并不能正确应用蛇格规则。但是我不确定,因此我问你最有可能工作的是什么。使用AD_UsersXAD_Groups
或AD_UserXAD_Group
因为您有一个“ x”,所以雄辩的魔术将永远无法自动匹配您的桌子。
您可以在用户模型的关系中覆盖表名。如果不是Eloquent期望的密钥“ group_id”和“ user_id”,您还可以指定密钥:
function groups() {
return $this->belongsToMany(GroupModel::class, 'AD_UsersXAD_Groups', 'user_id_key', 'group_id_key')
}
并且在您的组模型中,您可以执行此操作来扭转它
function users() {
return $this->belongsToMany(UserModel::class, 'AD_UsersXAD_Groups', 'group_id_key', 'user_id_key')
}