php - 如何使用 eloquent with 访问 laravel 中的表不直接附加

标签 php laravel eloquent

我有以下表结构,我想访问分配给特定案例但使用患者信息的门户。例如我有以下查询

$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      

最佳答案

假设您正确地创建了 PortalCasePatientOperator 模型之间的 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/

相关文章:

php - 对命令行 cURL 到 PHP 脚本调用感到困惑

php - Laravel 无法创建根目录

laravel - 如何获取laravel中表下不属于belongsToMany关系的用户列表?

mysql - 获取论坛中所有主题的回复总数

php - 在 Laravel-4 中批量插入具有关系的对象

php - 跟踪登台数据库中的更改然后将接受的数据推送到主数据库的好方法是什么?

php - 如何在 Zend Framework 2 中添加 Ajax 请求

php - Wordpress - 将联系表单 7 字段作为单独的列保存到数据库中

javascript - 获取 laravel 应用程序的语言环境

php - 如何在 laravel 中使用多个输入字段在 laravel 中实现搜索功能