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

nafeeur10's avatar

How to write Eager Loading in Query Laravel

$query->with(['batches'])->select(['name'])->orderBy('name', 'asc')->limit(1);

Result:

"batches": [
 {
      "id": 3079,
      "cost": 100,
      "stock": 20
 },
 {
      "id": 3080,
      "cost": 100,
      "stock": 20
}
]

I am getting the batches for the relationship with Products Model. But from batches I want to get all stock. How will I write my query?

0 likes
1 reply
Sergiu17's avatar
$query->with(['batches.stock'])
		->select(['name'])
		->orderBy('name', 'asc')
        ->limit(1);

Please or to participate in this conversation.