简体   繁体   English

Laravel 4表关系雄辩

[英]Laravel 4 tables relationship Eloquent

I have four tables: 我有四个表:

Users: id, username 用户:ID,用户名
Roles: id, name 角色:id,姓名
Domains id, title 网域编号,标题
DomainsAssignedRolesUsers id, role_id, user_id, domain_id DomainsAssignedRolesUsers id,role_id,user_id,domain_id

I want to get all user roles for a domain. 我想获取域的所有用户角色。

I do: 我做:

User::find(1)->domains->first()->roles

But i get all domains roles, not only for my user. 但是我获得了所有域的角色,不仅是我的用户。 Help me to get user roles only for the selected domain/ 帮助我仅获取所选域/的用户角色

My relations: 我的关系:

// User model:

public function rolesDomain() {
    return $this->belongsToMany('Role', 'domainsAssignedRolesUsers', 'user_id', 'role_id');
}

public function domains() {
    return $this->belongsToMany('Domain', 'domainsAssignedRolesUsers');
}

// Role model:

public function domains() {
    return $this->belongsToMany('Domain', 'domainsAssignedRolesUsers');
}

// Domain model:

public function roles() {
    return $this->belongsToMany('Role', 'domainsAssignedRolesUsers', 'domain_id', 'role_id');
}

public function users() {
        return $this->belongsToMany('User', 'domainsAssignedRolesUsers', 'domain_id', 'user_id');
    }

You want to get all the roles, from a specific domain, in relation with an user? 您想从特定域获得与用户有关的所有角色?

So this should do the trick: 因此,这应该可以解决问题:

User::find(1)->rolesDomain()->where('domain_id', $specificID)->get()

But if you want to only get the roles from the first domain for an user. 但是,如果您只想从用户的第一个域中获取角色。

User::find(1)->domains()->first()->roles()->get();

And if you only want to retrieve the roles for a user. 并且,如果您只想检索用户的角色。

User::find(1)->rolesDomain()->get() 

And if you only want to retrieve all the roles from each domain in relation with an user. 并且,如果您只想从每个域中检索与用户有关的所有角色。

User::find(1)->domains()->with('roles')->get()

Even if Eloquent documentation has a few example, This orm is really intuitive. 即使Eloquent文档中有一些示例,该orm还是非常直观的。

Check out Eloquent Triple Pivot (also on Packagist), it sounds like it does exactly what you want. 看看Eloquent Triple Pivot (也在Packagist上),听起来像它确实满足您的要求。

You'd set up your Models as User , Domain and Role (a User has many Domain s and can have a Role in each). 您将模型设置为UserDomainRole (一个User有多个Domain并且每个都有一个Role )。 Then look at the docs on the Github page (particularly step 6), and you'd define this on your Domain model: 然后查看Github页面上的文档(尤其是步骤6),然后在Domain模型中进行定义:

class Domain extends Eloquent {
    ...
    public function getRolesAttribute() {
        return $this->getThirdAttribute();
    }
}

Then you can simply call 然后,您可以简单地致电

$user = User::find(1);

$domain = $user->domains->first();
// or
$domain = $user->domains->find(73);

$roles = $domain->roles;

// Or the above can be condensed into just...
$roles = User::findOrFail( 1 )
             ->domains()
             ->findOrFail( 73 )
             ->roles;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM