【发布时间】:2020-02-20 00:05:36
【问题描述】:
我被困在两个数据透视表中,不知道如何执行此操作,因为我必须在 Laravel Yajara 数据表中显示数据。
首先,让我展示一下我的表结构。
---------------
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
在上面的表结构中,你可以发现,我有两个表(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
这是我的代码:
# 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');
}
在项目清单中,我想结合数据透视表并进行查询。
【问题讨论】:
-
你能给出你与模型的所有关系吗?对我们有帮助
标签: laravel eloquent pivot-table eloquent-relationship