我对拉拉维尔4.2的口才有点意见.首先,为表的命名而道歉。
有三张桌子:
这些表有一个映射表:
现在,我试图用特定的“区域”和归属"AccessRights“来显示所有用户。因此,我指定了我的模型如下:
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();现在雄辩地加载关于Userobject的所有信息..。在我的视图中,我想显示用户名、区域和归属权限。因为“区域”和"AccessRights“之间的归属我得到了两次相同的区域.但我只想为每个拥有集群权限的用户显示一次。例如:视觉示例
那么,我的问题是,我如何选择“用户”与他的归属,独特的领域,属于"AccessRights"?在控制器上是否有对结果进行排序的功能或可能性?或者我的用户模型有问题吗?我可以以任何可能的方式限制它吗?
我有点困惑..。
任何援助都是非常感谢的。
发布于 2017-08-23 09:24:24
我使用了多维数组
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循环来输出每个用户权限范围内的值。
我认为我在我的模型中的关系是不正确的,有一个更好的方法来获得这些结果。
我希望这对你也有帮助。
不管怎么说,如果有人知道更好的或另一种方式,我会很感激知道它。
https://stackoverflow.com/questions/45833055
复制相似问题