【问题标题】:Laravel 4.2 Eloquent belongsToMany three tables with one mappingtable possible resctrictionsLaravel 4.2 Eloquent belongsToMany 三个表和一个映射表可能的限制
【发布时间】:2018-01-31 15:36:28
【问题描述】:

我对 Laravel 4.2 中的 Eloquent 有一点问题...首先对表格命名感到抱歉。

一共有三个表:

  • 用户 -Id -Identification -Name -Username ...
  • SecurityAreas -Id -Identification -Description...
  • SecurityAccessRights -Id -Identification ... 仅包含两个数据集“读取”和“写入”

这些表有一个映射表:

  • SecurityMapping -Id -SecurityAccessAreasId -SecurityAccessRightsId -UsersId

现在我正在尝试显示具有特定“区域”和所属“访问权限”的所有用户。因此,我这样指定了我的模型:

User.php

class User extends Eloquent implements UserInterace, RemindableInterface {
    use UserTrait, RemindableTrait;


    protected $table = 'Users';
    protected $primaryKey = 'Id';
    public $timestamps = false;

    protected $hidden = array('password', 'remember_token');

    public function securityAreas() {
        return $this->belongsToMany('SecurityAreas', 'SecurityMappings', 'UsersId', 'SecurityAreasId' );
    }

    public function securityAccessRights() {
        return $this->belongsToMany('SecurityAccessRights', 'SecurityMappings', 'UsersId', 'SecurityAccessRightsId' , 'SecurityAreasId' );
    }
}

在我的 UserController.php 中,我试图获取用户。

$oUsers = User::with('securityAreas')->with('securityAccessRights')->get();

现在 Eloquent 加载用户对象上的所有信息......在我的视图中,我想显示用户名、区域和所属权限。 因为“区域”和“访问权限”之间的归属我两次获得相同的区域......但我想为每个拥有捆绑权限的用户显示一次。 例如: Visual Example

所以我的问题是如何选择“用户”及其所属的唯一区域和所属的“AccessRights”?是否有任何功能或可能性在控制器上对结果进行排序?或者我的用户模型有什么问题,我可以以任何可能的方式限制吗?

我有点糊涂了……

非常感谢任何帮助。

【问题讨论】:

    标签: php laravel eloquent mapping database-relations


    【解决方案1】:

    我使用了多维数组

    UserController.php
    
    $oUsers = User::with('securityAreas')->with('securityAccessRights')->get();
    foreach($oUsers as $oUser) {
      $arrUser = array();
    
      $arrUser['id'] = $oUser->Id;
      $arrUser['Name'] = $oUser->Name; 
      $arrUser['EMail'] = $oUser->EMail;
      $arrUser['ADUser'] = $oUser->Username;
      $arrUser['UserIdent'] = $oUser->Identification;
      $arrUser['SecurityAreas'] = array();
    
      $iCount = 0;
    
      foreach($oUser->securityAreas as $oArea) {
          if(!array_key_exists($oArea->Identification, $arrUser['SecurityAreas'])) {
              $arrUser['SecurityAreas'][$oArea->Identification] = array();
          }
          $arrUser['SecurityAreas'][$oArea->Identification][] = $oUser->securityAccessRights[$iCount]->Identification;
    
          $iCount++;
      }
    
      $arrReturn[] = $arrUser;
    }
    

    示例输出

    Array ( [0] => Array ( 
                          [Name] => User, My 
                          [EMail] => 
                          [Username] => MyUser 
                          [UserIdent] => 000000
                          [SecurityAreas] => Array ( 
                                                    [Area1] => Array ( 
                                                                      [0] => read
                                                                      [1] => write ) 
                                                    [Area2] => Array ( 
                                                                      [0] => read ) 
                                                    ) 
                           ) 
    )
    

    有了这个,我可以在我的视图中使用不同的 foreach 循环来为每个用户 - 区域 - 右侧输出正确范围内的值。

    我认为我的模型中的关系不正确,有更好的方法来获得这些结果。

    希望这对你也有帮助。

    无论如何,如果有人知道更好或其他方式,我将不胜感激。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-10
      • 2015-10-16
      • 2020-10-25
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 2021-08-30
      • 2016-11-10
      相关资源
      最近更新 更多