The query $result = Serial::where('date','2022-05-11')->with(['patient.village'])->get()
will result in a collection Serial where each item has a patient and each patient has a village (if they exist).
$serials = Serial::where('date','2022-05-11')->with(['patient.village'])->get()
foreach ($serials as $serial) P
dump($serial->patient->village);
}
First check your FK are correct between models and that the return relation is not inverted. Second, be careful to always select the FK. if you're doing DB query optimizations.
As an extra TIP: I prefer to write my eager loading like
$serials = Serial::with(['patient' => function ($q) { $q->with(['village']); }]) ->where('date', '2022-05-11') ->get();
this way you can add extra where conditions in your eager/lazy load queries
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community