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 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错