3 回答
TA贡献1796条经验 获得超10个赞
假设您正确地创建了 、 和 模型之间的 laravel 关系,则可以通过以下方式访问:PortalCasePatientOperator
// you get Case model
$case = \App\Case::findOrFail($case_id);
// patients: there is a hasMany relationship between Case and Patient models
$data['patients'] = $case->patients;
// there is a belongsTo relationship between Case and Portal models
$portal_name = $case->portal->Name;
TA贡献1777条经验 获得超3个赞
你必须使用另一种关系
$data['patients'] = Patient::with('operator', 'cases.portal')
->get();
在病人中 :
public function cases(){
return $this->hasMany(Case::class);
}
倘:
public function portal(){
return $this->belongsTo(Portal::class);
}
然后您可以使用以下命令访问它:
foreach($data['patients'] as $patient){
foreach($patient->cases as case){
$var = $case->name;
}
}
或者,如果您只想要所有门户名称:
$portals_names = $data['patient'][0]->pluck('cases.portal.name')
TA贡献1995条经验 获得超2个赞
Normaly,如果您在模型中执行了正确的关系,则只能使用:
$data['patients'] = Patient::with('operator')->where('case_id', $case_id)->get();
$portals = [];
foreach ($data['patients'] as $patient) {
foreach ($patient->cases as $case) {
$portals []= $case->portal;
}
}
我想在患者和病例之间有一个,在病例和门户之间有一个?HasManyBelongTo
如果要延迟加载数据,可以尝试:
$data['patients'] = Patient::with(['operator', 'cases.portal'])->where('case_id', $case_id)->get();
- 3 回答
- 0 关注
- 96 浏览
添加回答
举报