Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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      
like image 455
fazy Avatar asked Nov 22 '25 20:11

fazy


1 Answers

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;
like image 100
Giacomo M Avatar answered Nov 24 '25 09:11

Giacomo M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!