January 11th, 2023

lazy-eager-loading.png

Differences between lazy loading and eager loading

In Laravel, lazy loading and eager loading refer to the two ways in which related models can be loaded in a database query. In this article we will learn what is lazy and eager loading and when to use each of them.

Lazy Loading

Lazy loading is a technique where related models are not loaded at the same time as the main model, but are loaded only when they are accessed for the first time. This means that the related models are only loaded from the database as they are needed.

Lazy loading is beneficial when dealing with a large number of related models, as it can help to reduce the amount of memory used and improve the performance of the application. In Laravel, you can use the belongsTo or hasMany methods on the main model, followed by the load() method, to lazy load a related model.

For example, let's say you have a Post model and a Comment model, where a post can have many comments. To lazy load the comments for a post, you can use the following code:

1$post = App\Post::find(1);
2$comments = $post->comments()->get();

In this example, the comments() method returns a relationship object, which is not loaded until the get method is called. This allows you to access the comments for a post without loading them from the database until they are actually needed.

Another way to use lazy loading is using the whenLoaded() method, which allows you to access a related model only when it has been loaded. This can be useful when dealing with many models and you want to ensure that only the related models that have been loaded are accessed. Here is an example of how to use the whenLoaded() method

1$post = App\Post::find(1);
2$comments = $post->comments()->whenLoaded('comments')->get();

You can also use the load() method directly on a collection of models, to lazy load related models for multiple models at once. For example, if you want to load the comments for all posts, you can use the following code:

1$posts = App\Post::all();
2$posts->load('comments');

Laravel's lazy loading feature is a powerful tool that can help you to improve the performance of your application by reducing the amount of memory used and the number of database queries. It can be used with belongsTo, hasMany and hasOne relationships and you can use load(), get or whenLoaded() method to load the relationship in the desired moment. It also can be used on collections of models.

Keep in mind that Lazy loading is only useful if the relationship is not used on all cases and you really don't need to get that relationship all the time, otherwise you can use eager loading to avoid another query to the database and improve the performance.

Eager Loading

Eager loading is a technique where related models are loaded at the same time as the main model, using an additional query to the database. This approach can be beneficial when dealing with a small number of related models, as it can help to eliminate the need for additional queries and improve the performance of the application.

In Laravel, you can use the with method on the main model, followed by the name of the related model, to eager load a related model. The with method tells Laravel to include the specified related models in the same query as the main model, so that they are all loaded at the same time.

For example, let's say you have the same Post model and Comment. To eager load the comments for a post, you can use the following code:

1$post = App\Post::with('comments')->find(1);

In this example, the with() method is used to include the comments for the post in the same query as the post itself. This allows you to access the comments for a post without needing to run an additional query to load them.

You can also use the with() method on multiple related models, by passing an array of the related models to the method:

1$post = App\Post::with(['comments', 'author'])->find(1);

This will load the post, its comments, and the author in one query.

You can also use the with() method on a collection of models, to eager load related models for multiple models at once. For example, if you want to load the comments for all posts, you can use the following code:

1$posts = App\Post::with('comments')->get();

In addition to this, Laravel also allows you to specify constraints on the eager loaded relations, by passing a closure to the with() method. Here is an example of how you can use constraints to load comments that are only approved.

1$post = App\Post::with(['comments' => function($query){
2 $query->where('approved', true);
3}])->find(1);

In conclusion, Laravel's eager loading feature is a powerful tool that can help you to improve the performance of your application by eliminating the need for additional queries. You can use the with() method to load the relationship in the same query as the model, and also you can use closures to apply constraints on the relationship. Keep in mind that eager loading is not the best choice when you have a large number of related models, in that case you can use lazy loading to avoid loading all the related models at once and use memory efficiently.

Conclusion

Whether you should use lazy loading or eager loading in your Laravel application depends on the specific use case and the number of related models you need to load. Lazy loading can be more memory efficient for larger number of related models, and eager loading can improve performance when dealing with a small number of related models.

fathom analytics Follow @LaravelMagazine on Twitter →

Comments

Marian Pop

PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

Newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox every Friday!

We'll never share your email address and you can opt out at any time.