通过在拉拉维尔的多对多关系通过多元化交换获得遥远的关系 [英] Retrieve distant relation through has-many-through for many-to-many relation in Laravel
问题描述
我的应用程序中有以下型号
- 用户
- 组
- 任务
其中具有以下关系
- 用户和组具有多对多关系
- 任务和组具有多对多关系
所以基本上一个用户可以属于多个组,每个组可以有多个任务。
以下是表格结构。
用户
- id
- name
groups
- id
- 名称
任务
- id
- 名称
group_user
- id
- group_id(带有表的外键)
- user_id(带用户的外键s表)
group_tasks
- id
- group_id(带有表的外键)
- task_id(带有任务表的外键)
现在我想检索用户的所有任务。
我尝试了以下方法并且两者都不起作用。
方法1
-
$ user-> groups()
给出用户的组列表 -
$ group-> tasks()
给出组的任务列表
所以我尝试了
$ user-> groups() - > tasks()
但没有起作用。 >
方法2
我尝试了有许多通过将其添加到我的用户模型中
public function tasks()
{
return $ this-> hasManyThrough(Task :: class,Group :: class);
}
但即使没有工作。以下是我得到的错误
Connection.php行713中的QueryException:
SQLSTATE [42S22]:列未找到:1054'字段列表'中的未知列'groups.user_id'(SQL:选择`tasks`。*,`groups`.`user_id从`tasks`内部连接`groups`在`groups`.`id `=`tasks`.`group_id`其中`groups`.`user_id` = 1)
我的猜测是这样的,因为它期待着一对多的关系,但是我有多对多关系。
那么有一种方法来检索
用户模型
public function groups()
{
return $ this-> belongsToMany('App \Group');
}
组模型
public function tasks()
{
return $ this-> belongsToMany('App \Task );
}
任务模型
public function groups()
{
return $ this-> belongsToMany('App \Group );
}
检索用户的所有任务。
$ user = User :: find(1);
$ user-> load('groups.tasks');
$ tasks = $ user-> groups-> pluck('tasks') - > collapse();
I have the following models in my application
- User
- Group
- Task
which have the following relationships
- User and Group have a many-to-many relationship
- Task and Group have a many-to-many relationship
So basically a user can belong to more than one group and each group can have more than one task.
Following is the table structure.
users
- id
- name
groups
- id
- name
tasks
- id
- name
group_user
- id
- group_id (foreign key with groups table)
- user_id (foreign key with users table)
group_tasks
- id
- group_id (foreign key with groups table)
- task_id (foreign key with tasks table)
Now I want to retrieve all the tasks for the user.
I tried the following approaches and both didn't work.
Approach 1
$user->groups()
gives the list of groups for a user$group->tasks()
gives the list of tasks for a group
So I tried
$user->groups()->tasks()
but it didn't work.
Approach 2
I tried Has Many Through by adding this to my User model
public function tasks()
{
return $this->hasManyThrough(Task::class, Group::class);
}
but even that didn't work. The following is the error that I am getting
QueryException in Connection.php line 713:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'groups.user_id' in 'field list' (SQL: select `tasks`.*, `groups`.`user_id` from `tasks` inner join `groups` on `groups`.`id` = `tasks`.`group_id` where `groups`.`user_id` = 1)
My guess is that this is happening because it is expecting one-to-many relationship, but I have a many-to-many relationship.
So is there a way to retrieve it without getting all groups and then looping through them?
User Model
public function groups()
{
return $this->belongsToMany('App\Group');
}
Group Model
public function tasks()
{
return $this->belongsToMany('App\Task');
}
Task Model
public function groups()
{
return $this->belongsToMany('App\Group');
}
Retrieving all tasks for a user.
$user = User::find(1);
$user->load('groups.tasks');
$tasks = $user->groups->pluck('tasks')->collapse();
这篇关于通过在拉拉维尔的多对多关系通过多元化交换获得遥远的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!