What is the difference between load() and with() in Eloquent?
Why Interviewers Ask This
Senior Laravel engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.
Answer
Both load relationships but at different stages. with() is called on the query before execution — it is eager loading, including the relationship data in the initial query. It executes 2 queries: one for the main models, one for all related models. $posts = Post::with("author")->get(). load() is called on an already-retrieved collection or model — it is lazy eager loading, loading the relationship after the fact. $posts->load("author") executes one additional query after you already have the posts. Use with() when you know in advance you will need the relationship. Use load() when you have already retrieved models and conditionally need to load a relationship (e.g., an API resource that loads relationships based on request parameters). Both prevent N+1; load() offers flexibility at the cost of an extra query round-trip.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Laravel project, I used this when...' immediately makes your answer more credible and memorable.