doulingqiu4349 2014-11-30 16:31
浏览 14
已采纳

Laravel 4中具有特定角色的用户的额外字段

I'm developing an application in Laravel 4.2 with a MySQL DB. I have the 'users', 'roles_users', and 'roles' tables, with 'role_user' being the pivot table because of the many-to-many relationship. Now, in my application there will be different types of accounts. Some types need to have an extra field unique to them. That field is a foreign key field, linking the type of account to a specific table.

Example:

  • Type A Account: has access to all areas
  • Type B Account: has access to only 1 area and its sub-areas.
  • Type C account: has access to only 1 sub-area.

So, there would be an 'areas' table and a 'sub-areas' table, with extra information. For Account Type A, I would need no extra fields. For B, I would need an 'area_id' field, and for C a 'sub-area_id' field. Where would those fields go? I don't think it would be the 'users' table, since they aren't common fields.

If you need extra information, let me know and I'll do my best to help.

  • 写回答

2条回答 默认 最新

  • duanqing3026 2014-12-01 11:52
    关注

    If you want to this cleanly and to expand more in the future, what I would do is create 2 more tables, a permissions table and a permission_role many-to-many table.

    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();
    

    So you would assign several permissions to each role. And there would be 3 roles (e.g. account_a, account_b, account_c). One weakness would be, if you add a new subarea in an area, e.g. account_a would not automatically have access to it. It would be a bit more complex, but if you want to automate when account_a has access to area x and all of its subareas (e.g. x.1, x.2, x.3), and there's a new subarea x.4 and you would account_a to automatically gain access, you can do this (polymorphic-ish many-to-many):

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了