Support the ongoing development of Laravel.io →
Eloquent
Last updated by @athar-jamil-raihan 2 years ago.
0
moderator

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);
}
0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.