Search code examples
sqllaravelormeloquenteager-loading

Laravel Eager Loading - Always Good?


When creating a query with the Laravel Eloquent ORM (i.e. select * from mytable...) is it ever preferrable not to eager load? Obviously eager loading is better for performance, but does it have any downsides?

Thanks


Solution

  • The potential benefit of lazy loading, (i.e. - not eager loading), is the same as eager loading, namely performance. Lazy loading can improve the overall speed of your application in situations where it's likely that you won't require and/or won't be accessing related models. Similarly, eager loading would be the right choice when you're more likely to require related models.

    In my experience, given the overhead of additional queries, I would save lazy loading for situations where you're fairly unlikely to require the additional models.