[英]How to combine two pivot tables in Laravel?
I am stuck in two pivot table and not getting any idea how to do this as I have to display data in Laravel Yajara data table.我被困在两个 pivot 表中,不知道如何执行此操作,因为我必须在 Laravel Yajara 数据表中显示数据。
First, let me show you my table structure.首先,让我向您展示我的表结构。
---------------
Table: projects
---------------
Column Type
id int(10)
uid char(36)
project_name varchar(255)
created_by int(10)
updated_by int(10)
created_at timestamp NULL
updated_at timestamp NULL
deleted_at timestamp NULL
--------------------
Table: group_project
--------------------
Column Type
group_id int(10)
project_id int(10)
-------------
Table: groups
-------------
Column Type
id int(10)
uid char(36)
group_name varchar(255)
created_by int(10)
updated_by int(10)
created_at timestamp
updated_at timestamp
deleted_at timestamp
-----------------
Table: group_user
-----------------
Column Type
group_id int(10)
user_id int(10)
-------------
Table: users
-------------
Column Type
id int(10)
uid char(36)
name varchar(255)
first_name varchar(255)
last_name varchar(255)
email varchar(255)
phone varchar(255)
password varchar(255)
remember_token varchar(100)
created_at timestamp
updated_at timestamp
deleted_at timestamp
In above table structure you can find that, I have two tables(group_user, group_project) which are in relation from project to group and group to user and I want to get records like this.在上面的表结构中,您可以发现,我有两个表(group_user,group_project),它们从项目到组以及从组到用户,我想获得这样的记录。
Project 1
|
-- Group 1
|
-- User 1
-- User 2
-- Group 2
|
-- User 3
-- User 4
Project 2
|
-- Group 1
|
-- User 1
-- User 2
-- Group 3
|
-- User 5
-- User 6
Here is my code:这是我的代码:
# Project Controller
public function index()
{
$projectsObj = $this->project->with(['projectGroups'])->get();
}
# Project Model
public function projectGroups()
{
return $this->belongsToMany('App\Groups', 'group_project', 'project_id', 'group_id');
}
In the project listing, I want to combine both the pivot table and make a query.在项目清单中,我想结合 pivot 表并进行查询。
The following will return the structure you want.以下将返回您想要的结构。
Projects::with('projectGroups', 'projectGroups.users');
If you need to limit the query at all, add constraints to the withs如果您完全需要限制查询,请在 withs 中添加约束
Project::with([
'projectGroups' => function($query){
$query->where('group_name', 'MyName');
},
'projectGroups.projects' => function ($query) {
$query->where('project_name', 'my_name');
}])->get()
In Project model change the relationship as在项目 model 中,将关系更改为
public function projectGroups()
{
return $this->hasMany('App\ProjectGroup', 'project_id', 'id');
}
In model GroupProject model集团项目
public function group()
{
return $this->belongsToOne('App\Group', 'group_id', 'id');
}
In Group model组内 model
public function groupusers()
{
return $this->hasMany('App\GroupUser', 'group_id', 'uid');
}
In GroupUser model组内用户 model
public function users()
{
return $this->hasMany('App\User', 'user_id', 'uid');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.