我有以下表结构,我想访问分配给特定案例但使用患者信息的门户。例如我有以下查询
$data['patients'] = Patient::with('operator')->where('case_id', $case_id)->get();
此查询现在返回分配给患者的运算符(operator),我希望使用案例分配给患者的门户名称。
入口表
id Name
1 A
2 B
案例
id Name case_number patient_name user_id portal_id
1 Farz 456 sania 5 1
病人
id case_id operator_id
2 456 5
最佳答案
假设您正确地创建了 Portal
、Case
、Patient
和 Operator
模型之间的 laravel 关系,您可以以这种方式访问:
// 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;
关于php - 如何使用 eloquent with 访问 laravel 中的表不直接附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60282272/