我在数据库中有一个用户员工表,如下所示
id | name | email | pic
1 Michael [email protected] 4
2 John Doe [email protected] 4
3 Lorem Ipsum [email protected] 4
4 Dolor Amet [email protected] 5
5 Campus Criteria [email protected]
我想在用户 ID 5 登录时显示数据,他可以查看他的团队,用户 ID 5 是用户 ID 4 的图片,用户 ID 4 是用户 ID 1、2 和 3 的图片
这是我想要的
Dolor Amet
-----------------------------------------------
Michael [email protected]
John Doe [email protected]
Lorem Ipsum [email protected]
我曾尝试使用紧凑代码,但它只显示用户 ID 4,而不是用户 ID 1,2 和 3
这是我现在的视图,它只查看用户 ID 4,用户 ID 1,2 和 3 不显示
Dolor Amet
-----------------------------------------------
这是我的控制器代码
public function user($id)
{
$pic=Users::where('pic',$id)->get();
foreach($pic as $pics)
{
//dd($pics->id);
$use=Users::where('pic',$pics->id)->get();
}
return view('user',compact(['pic','use']));
}
这是我的刀片代码
<div class="col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Your Team</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
@foreach($pic as $pics)
<thead class=" text-primary">
<th class="text-center">{{ $pics->name }}</th>
</thead>
@endforeach
<tbody>
@foreach($use as $uses)
<tr>
<td>{{$uses->name}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
你知道失踪在哪里吗?
谢谢
发生这种情况是因为每次循环执行时旧集合都替换为新集合
试试这个
$use = array();
foreach($pic as $pics) {
$use[] = Users::where('pic',$pics->id)->get();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句