我正在使用 MySQL 数据库在 Laravel 4.2 中开发一个应用程序。我有“users”、“roles_users”和“roles”表,由于多对多关系,“role_user”是数据透视表。现在,在我的应用程序中将有不同类型的帐户。有些类型需要有一个独特的额外字段。该字段是外键字段,将帐户类型链接到特定表。
示例:
- A 类帐户:可以访问所有区域
- B 类帐户:只能访问 1 个区域及其子区域。
- C 类帐户:只能访问 1 个子区域。
因此,将会有一个“区域”表和一个“子区域”表,其中包含额外信息。对于帐户类型 A,我不需要额外的字段。对于 B,我需要一个“area_id”字段,对于 C,我需要一个“sub-area_id”字段。这些田地会去哪里?我认为这不会是“用户”表,因为它们不是公共(public)字段。
如果您需要更多信息,请告诉我,我会尽力提供帮助。
最佳答案
如果您想干净地做到这一点并在将来扩展更多,我要做的就是再创建 2 个表,一个权限表和一个permission_role 多对多表。
for permissions table:
$table->increments('id');
$table->integer('area_id')->unsigned();
$table->integer('subarea_id')->unsigned();
for permission_role table:
$table->increments('id');
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
因此,您可以为每个角色分配多个权限。将会有 3 个角色(例如 account_a、account_b、account_c)。一个弱点是,如果您在一个区域中添加一个新的分区,例如account_a 不会自动访问它。这会有点复杂,但如果您想在 account_a 有权访问区域 x 及其所有子区域(例如 x.1、x.2、x.3)时实现自动化,并且有一个新的子区域 x.4,您将account_a 自动获得访问权限,你可以这样做(多态多对多):
for permissions table:
$table->increments('id');
$table->string('area_type'); // i.e. either "Area" or "Subarea"
$table->integer('area_access_id')->unsigned(); // this can be an ID to an "Area" or to a "Subarea"
for permission_role table:
$table->increments('id');
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
for area table:
$table->increments('id');
$table->string('name');
for sub_area table:
$table->increments('id');
$table->integer('parent_area_id')->unsigned(); // references area.id
$table->string('name');
关于php - Laravel 4 中具有特定角色的用户的额外字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215454/