How to get data from parent and child table on the basis of status where foreign key have different status for every row

此生再无相见时 提交于 2021-02-17 06:52:18

问题


I have 2 tables with foreign key relationship. Situation is I have a case and a case have many revisions and every revision have its own status. I want to get parent table data and child data if only specific row of foreign key table status is changed

Table Case

id      case_no    patient_name  age
1       12564        abc         78
2       1256         lkj         63
3       125          bdhf        23

Table Case_Revisons

    id      case_id    revison         status
    1         1           0           assesemnt
    2         1           1           assesment
    3         1           2           treatment
    4         2           2           assesment
    5         3           1           assesment

What I want is all data all data from Case and Case Revisions table where status is treatment

What I tried:

$data['treatment_setup_cases'] = MedicalPrimaryCases::with('primaryCaseNo')
            ->where('impression_type', 1)
            ->where('status', 'treatment-setup')
            ->get();


 public function primaryCaseNo()
    {
        return $this->belongsTo(PrimaryCaseNo::class, 'primary_medical_case_id');
    }

回答1:


Use a join here:

$the_data_you_want = Case::join('case_revisions', 'case.id', '=', 'case_revisions.case_id')
    ->where('status', 'treatment')
    ->get();

PrimaryCaseNo and impression_type were not mentioned in the explanation of the question so I ignored them, you can change this if necessary.

I hope this helps you.



来源:https://stackoverflow.com/questions/60639621/how-to-get-data-from-parent-and-child-table-on-the-basis-of-status-where-foreign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!