如何解决如何获取数据并使用外键在laravel中联接两个表
医疗床
患者表
医学模型
public function patients() {
return $this->belongsTo(Patient::class);
}
患者模型
public function medicals() {
return $this->hasMany(Medical::class) ;
}
MedicalController
public function index()
{
$medicals=Medical::all();
return view('main.medicalrecord.index',compact('medicals'));
}
medicalrecord.index查看
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><strong>Patient No.</strong></th>//from the Patient table
<th><strong>First Name</strong></th>//from the Patient table
<th><strong>Middle Name</strong></th>//from the Patient table
<th><strong>Last Name</strong></th>//from the Patient table
<th><strong>Smoke</strong></th>//from the Medical table
</tr>
</thead>
<tbody>
@if($medicals)
@foreach($medicals as $medical)
<tr>
<td>{{$medical->patient_id}}</td>
<td></td>
</tr>
@endforeach
@endif
</tbody>
</table>
嗨,我使用的外键是$ table-> foreign('patient_id')-> references('id')-> on('patients');在医疗移民中。
简而言之,如何在我的视图中将id,名字,中间名,姓氏和病人DB的数据获取到医疗表中,简而言之,如何用外键将病人表和医疗表这两个表连接起来
解决方法
医学模型
public function patient() {
return $this->belongsTo(Patient::class);
}
您可以使用patient
关系从此类医疗实例访问患者的数据。
您认为
@if($medicals)
@foreach($medicals as $medical)
<tr>
<td>{{$medical->patient->id}}</td>
<td>{{$medical->patient->firstname}}</td>
<td></td>
</tr>
@endforeach
@endif
此外,如果您希望将患者关系加载到控制器中,那就更好了。
$medicals=Medical::with('patient')->get();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。