【发布时间】: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