Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Arbnor's avatar

Laravel Eloquent - Retrieve data from pivot table

I'm trying to retrieve data from pivot table with where condition.

I have three tables:

  1. reports: id, patient_id, outcome
  2. analyzes: id, name, referenceValue
  3. analyzes_reports(pivot table): analyzes_id, reports_id

My Report.php model:

class Report extends Model { protected $table = 'reports'; protected $fillable = ['patient_id', 'outcome'];

public function patients()
{
    return $this->belongsToMany('App\Patient');
}
public function analyzes()
{
    return $this->belongsToMany('App\Analysis','analyzes_reports', 'analyzes_id', 'reports_id');
}

}

My Analysis.php model:

class Analysis extends Model { protected $table = 'analyzes'; protected $fillable = ['name', 'referenceValue']; public function reports() { return $this->belongsToMany('App\Report','analyzes_reports', 'analyzes_id', 'reports_id'); } }

My Patient.php model:

class Patient extends Model { // protected $table = 'patients'; protected $fillable = ['name', 'surname','tel'];

public function reports()
{
    return $this->hasMany('App\Report');
}

}

Now, I want to retrieve data for example like this:

reports.id | patient.name | analyzes.name 1 John leukocytes 1 John erythrocyte 2 John erythrocyte 2 John glucose

So, one report with a certain patient may have multiple kinds of analyzes, because a patient can make more than one kind of analyzes.

The question is, how to retrieve data for example for John:

Report(1) patient(John) analyzes(leukocytes,erythrocyte) Report(2) patient(John) analyzes(erythrocyte,glucose)

0 likes
9 replies
Snapey's avatar
$patient = Patient::with('reports.analyzes')->find(1);
1 like
Arbnor's avatar

Thank you, but this didn't work. In my blade I tried to get data with foreach loop:

     @foreach($patient as $value)           
     {{$value->id}}  // Or whatever
     @endforeach 

an error appeared: Invalid argument supplied for foreach()

I tried also without foreach: {{$patient->id}}

an error appeared: Trying to get property of non-object

Snapey's avatar

did you pass $patient to view?

did you provide a valid patient id in the find statement?

Arbnor's avatar

Yes of course, I passed it with: return view('patient',['patient' => $patient]); My patients table are populated with valid id, in this case I have added a record with id=1

chatty's avatar
 @foreach($patient->reports as $report)           
     {{$report->id}}  // Or whatever
     @endforeach 
Arbnor's avatar

@shig

Thank you it works. It show id of reports for a patient(ex. patient(1) reports(1,2)). Now, how to fetch name of analyzes from analyzes table in this loop. I tried:

{{$report->name}} // this shows: Blank value
{{$report->analyzes->name}} //Error: Property [name] does not exist on this collection instance. 
Sergiu17's avatar

@Arbnor I suggest you to take small steps.

// 1
$patient = Patient::first();
dd($patient);
// if it works, next step. 

// 2
$patient = Patient::with('reports')->first();
dd($patient->reports);
// if this doesn't return reports, make sure there are reports.

And so one, continue till you get what you want.

// to fetch analyzes, I took the loop from @shig comment.

@foreach($patient->reports as $report)           
    {{ $report->id }}  // Or whatever
    
    @foreach($report->analyzes as $analyze)
        {{ $analyze->id }}
    @endforeach
@endforeach 
Arbnor's avatar

@Sergiu17

from this loop that you wrote, the data witch fetched is only $report-id. The second foreach loop doesn't show me any data even though I have populated the pivot table manually

Kentama's avatar

Good day, Sir. I am wondering if my case here is sort of achievable by this kind of solution. May I ask for some help from you, my kind Sir?

Please or to participate in this conversation.