問題描述
在 Laravel 5.2 應(yīng)用程序中,我有三個(gè)模型:User
、Role
和 Task
.一個(gè)User
關(guān)聯(lián)多個(gè)Roles
,一個(gè)Role
關(guān)聯(lián)多個(gè)Tasks
.因此,每個(gè)用戶都通過他們的角色與多個(gè)任務(wù)相關(guān)聯(lián).
In a Laravel 5.2 application I have three models: User
, Role
and Task
.
A User
is associated with multiple Roles
, and a Role
is associated with multiple Tasks
.
Therefore each user is associated to multiple tasks, through their roles.
我正在嘗試通過他們的角色訪問與 User
關(guān)聯(lián)的所有 Tasks
.
I am trying to access all Tasks
associated with a User
, through their Roles.
我的模型的相關(guān)部分如下所示:
The relevant parts of my models look like:
class User extends Authenticatable
{
public function roles()
{
return $this->belongsToMany('AppRole');
}
public function tasks()
{
return $this->hasManyThrough('AppTask', 'AppRole');
}
}
class Role extends Model
{
public function tasks()
{
return $this->belongsToMany('AppTask');
}
public function users()
{
return $this->belongsToMany('AppUser');
}
}
class Task extends Model
{
public function roles()
{
return $this->belongsToMany('AppRole');
}
}
以下返回SQL錯(cuò)誤;
Column not found: 1054 Unknown column 'roles.user_id'
它似乎試圖通過 Role 模型中的(不存在的)外鍵訪問關(guān)系,而不是通過數(shù)據(jù)透視表.
It seems to be trying to access the relationship through a (non-existent) foreign key in the Role model, rather than through the pivot table.
$user = Auth::user;
$tasks = $user->tasks;
如何通過這些關(guān)系訪問與用戶相關(guān)的所有任務(wù)?
How can I access all tasks related to a user through these relationships?
推薦答案
我開發(fā)了一個(gè)自定義BelongsToManyThrough
關(guān)系,您可能會(huì)感興趣.您需要添加新的關(guān)系類(在我的要點(diǎn)中給出;粘貼在這里太長(zhǎng)了),并且還需要按照要點(diǎn)中的描述覆蓋您的基本 Model
類以實(shí)現(xiàn) belongsToManyThrough
.
I have developed a custom BelongsToManyThrough
relationship which might interest you. You would need to add the new relation class (as given in my gist; it is too long to paste here), and also override your base Model
class as described in the gist to implement belongsToManyThrough
.
然后(假設(shè)您使用 Laravel 的默認(rèn)表命名方案 - 如果沒有,您也可以指定連接表),您可以將關(guān)系定義為:
Then (assuming you are using Laravel's default table naming schemes - if not, you can specify the joining tables as well), you would define your relationship as:
public function tasks()
{
return $this->belongsToManyThrough(
'AppTask',
'AppRole');
}
belongsToManyThrough
不僅會(huì)為您提供用戶的任務(wù)列表,還會(huì)告訴您每個(gè)用戶通過其擁有每個(gè)任務(wù)的角色.例如,如果您有:
belongsToManyThrough
will not only give you a list of Tasks for your User(s), it will also tell you the Role(s) via which each User has each Task. For example, if you had:
$user->tasks()->get()
輸出將類似于:
[
{
"id": 2,
"name": "Ban spammers",
"roles_via": [
{
"id": 2,
"slug": "site-admin",
"name": "Site Administrator",
"description": "This role is meant for "site administrators", who can basically do anything except create, edit, or delete other administrators."
},
{
"id": 3,
"slug": "group-admin",
"name": "Group Administrator",
"description": "This role is meant for "group administrators", who can basically do anything with users in their same group, except other administrators of that group."
}
]
},
{
"id": 13,
"name": "Approve posts",
"roles_via": [
{
"id": 3,
"slug": "group-admin",
"name": "Group Administrator",
"description": "This role is meant for "group administrators", who can basically do anything with users in their same group, except other administrators of that group."
}
]
},
{
"id": 16,
"name": "Reboot server",
"roles_via": [
{
"id": 2,
"slug": "site-admin",
"name": "Site Administrator",
"description": "This role is meant for "site administrators", who can basically do anything except create, edit, or delete other administrators."
}
]
}
]
我的自定義關(guān)系有效地完成了這項(xiàng)工作,只需要幾個(gè)查詢,而不是其他涉及 foreach
的解決方案,后者會(huì)創(chuàng)建一個(gè) n+1 查詢 問題.
My custom relationship does this efficiently, with only a few queries, as opposed to other solutions involving foreach
, which would create an n+1 query problem.
這篇關(guān)于Laravel 5 雄辯的 hasManyThrough/belongsToManyThrough 關(guān)系的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!