【发布时间】:2014-02-23 09:29:46
【问题描述】:
我有 3 个模型:User、Role、Tool,其中每个用户可以有多个角色,每个角色可以有很多工具。
多对多关系在每种情况下都运作良好。我可以访问:
User::find(1)->roles
Tool::find(1)->roles
Role::find(1)->tools
Role::find(1)->users
我的桌子是:
users
id
name
roles
id
name
tools
is
name
role_user
id
role_id
user_id
role_tool
id
role_id
tool_id
在每个模型中:
//In User Model
public function roles()
{
return $this->belongsToMany('Rol');
}
//In Role Model
public function users()
{
return $this->belongsToMany('User');
}
public function tools()
{
return $this->belongsToMany('Tool');
}
//In Tool Model
public function roles()
{
return $this->belongsToMany('Rol');
}
我需要获得单个用户的所有工具,例如:User::find(1)->roles()->tools。我该怎么做?
【问题讨论】:
标签: php laravel laravel-4 many-to-many pivot-table