How to access table in laravel using eloquent with is not directly attached

fazy :

I have the following table structure and i want to access Portal which is assigned to specific case but using patient info. For example i have the following query

$data['patients'] = Patient::with('operator')->where('case_id', $case_id)->get();

this query returns the operator assigned to the patient now hare i want the portal name assigned to the patient using case.

Portal Table

id   Name   
1   A
2   B

Cases

id     Name    case_number     patient_name     user_id     portal_id
1      Farz       456            sania             5           1    

Patient

id    case_id   operator_id
2     456            5      
Giacomo M :

Assuming you properly created the laravel relationships among Portal, Case, Patient and Operator models, you can access in this way:

// 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;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=9796&siteId=1