Load relationships deliberately

Take your time, follow the code and check your understanding.

The codeLaravel routing and validation
 $orders = Order::all();
 foreach ($orders as $order) {
    echo $order->customer->name;
 }
Your answer4 min · no timer
What needs fixing?

Show solution and explanation

Accessing an unloaded relationship in a loop can cause one query per order. Eager loading fetches the relationship for the collection deliberately.

Consider eager loading with Order::with("customer")->get().