laravel - 如何通过 2 表 FK 在刀片上显示数据
问题描述
我有 3 个表,例如users、data_users、practice
users : id , name ,dob ,email , etc . . .
data_users : id , user_id , number_exp , etc . .
practice : id , data_user_id , practice_number , etc ....
在 3 个表中,通过关系 belongsTo 连接并且想要显示这个。我想显示有关表练习的数据:
我的控制器:
public function show()
{
$practice= Practice::with('data_users')->get();
return view('admin.practice' ,['practice'=>$practice]);
}
我的观点
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>No Surat</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@foreach ($practice as $i)
<tr>
<th scope="row">1</th>
<td>{{ $i->data_users->users->name}}</td>//i want to show name but i cant show this
<td>{{ $i->practice_number}}</td>
<td>{{ $i->practice_date}}</td>
<td>{{ $i->status}}</td>
</tr>
@endforeach
</tbody>
</table>
我在显示数据“名称”表格用户时遇到问题,我只能 从data_users显示id
但仍然错误尝试获取财产
解决方案
您正在$practice
从控制器返回集合,试图 foreach$riwayat
集合。
推荐阅读
- postgresql - 即使使用 adminer 或 sql 命令 CONSTRAINT DOESN'T EXIST 也无法使用 postgres 删除约束
- android - Unable to find certain views in my .axml layout file
- javascript - 如何仅制作头部木偶镀铬视图
- flutter - 如何在 Flutter 中构建此布局/UI?
- sql - Oracle SQL 数据获取顺序
- docker - 在 Swarm 更新期间耗尽节点时,如何避免新的活动节点接收所有重新调度的容器?
- c# - 将从字符串获得的自定义类型传递给具有泛型类型的函数
- python - 如何将字典列表转换为元组列表
- python - 是否可以为正在运行的 Python 程序设置文件权限
- python-3.x - Python 请求:分块发布请求