我创建了一个名为Team和team_user的两个表。这是两个表的模式
Team Table
---------------------------------
| id | team_name | user_id |
---------------------------------
| 1 | Developer | 5 |
| 2 | Designer | 6 |
| 3 | Test | 7 |
| 4 | Test 2 | 8 |
---------------------------------
Team_User Table
-------------------------------------------------
| id | team_id | tl_id | user_id |
-------------------------------------------------
| 1 | 1 | 5 | 25 |
| 2 | 2 | 6 | 35 |
| 3 | 2 | 7 | 12 |
| 4 | 3 | 8 | 13 |
-------------------------------------------------
tl_id (Team Leader User ID)
user_id (User ID of user who is under Team leader)
团队负责人可以在其下拥有多个用户。我想在“组长”视图中显示其团队成员的所有职位。
例如,如果名为abc的团队负责人下有一个名为def的成员,而另一个名为ghi的人则在def下。因此,我想在abc Leaders视图下显示def和ghi的所有帖子。
到目前为止,我已经做到了-
$user = auth()->user();
$teams = DB::table('team_user')->where('tl_id', $user->id)->get();
$tl = array();
foreach ($teams as $team) {
$tl[] = $team->user_id;
}
$tl[] = auth()->user()->id;
$posts = DB::table('post')->where('status',1)->Where(function ($query) use($tl) {
for ($i = 0; $i < count($tl); $i++){
$query->orwhere('user_id', '=', $tl[$i]);
}
})->get();
从上面的代码中,我只能在abc下获取def的发布列表。我怎么也可以在他的下面找到这个帖子?
答案 0 :(得分:0)
我已经通过这种代码逻辑得到了解决方案
$user = auth()->user();
$teams = DB::table('team_user')->where('tl_id', $user->id)->get();
$tl = array();
foreach ($teams as $team) {
$tl[] = (int)$team->user_id;
$searches = DB::table('team_user')->where('tl_id',$team->user_id)->get();
foreach($searches as $search){
$tl[] = (int)$search->user_id;
$searched = DB::table('team_user')->where('tl_id',$search->user_id)->get();
foreach($searched as $searchd){
$tl[] = (int)$searchd->user_id;
}
}
}