DEV Community

alamriku
alamriku

Posted on

Lazy loading, lazy collection in Laravel.

Understanding of Relational Database And Eloquent ORM of laravel is mandatory to understand this blog post please follow

eloquent-relationships

1 Lazy Loading :

Generally we can say, Lazy Loading works on a website or webpage for delaying load or initialization of resources or objects.
Example when we visit facebook its load first time show us some gray images then its load the our timeline.

1 Lazy Loading : In Laravel Lazy Loading can be seen on two ways but both are nuance.

Let us suppose a relationship: Users and Posts.

a User can have multiple Posts
a Post can have single User
Here we can say User is parent table for Posts.
Posts is the child table for Users table.

$users = User::get();
Enter fullscreen mode Exit fullscreen mode

Here we are getting all the users. Now when we loop through the users and accessing the post of that user we are doing lazy loading.

Below syntax is just for visualization $user(3) its not allowed in php.
foreach($users(3)  as $user){
$user->post;
}
Enter fullscreen mode Exit fullscreen mode

Dynamic relationship properties perform "lazy loading", meaning they will only load their relationship data when you actually access them.
Here we find "N + 1" query problem.
suppose we have three users so N = $user =User->get() = 3 collections

foreach($users  as $user){
$user->post;
}
Enter fullscreen mode Exit fullscreen mode

The above loop will loop for three times. N = 3. $user->post query will be called three times so we have three queries and a single query for getting the $users = User->get().
User->get() = 1
$user->post = N times. so we have N+1 problem.
Reference : relationship-methods-vs-dynamic-properties

2 Lazy Loading: This can be explain when we do relationship from child to parent table.

$posts = Post::get().
To get the user for every post we will do loop.
Post is child table for User parent table. Here we also find "N + 1" query problem.

To alleviate N+1 we use eager-loading in laravel. The with() method give us the power to do eager-loading.

$users = User::with("posts")->get();
Enter fullscreen mode Exit fullscreen mode

behind the scene the with("posts") do a where...in.. query on mysql.

Lazy Collection in laravel or overall concept:

Lazy collection works for a big dataset like multi gigabytes file sizes. For Example, if you want to read data from a big size file it can exceed your memory size. SO lazy collections can be used for your reading the data from your file in small parts.

Laravel cursor method on Eloquent ORM give us the lazy collection. The cursor behind the scene uses the PHP generator.

Laravel cursor gives us lazy collections

LazyCollection::make(function () { } ) also create lazy collecitons

Flight::lazy() also give us lazy collections.

the lazy method returns a flattened LazyCollection of Eloquent models.

Reference : streaming-results-lazily
Reference : lazy-collections

Thank you for reading my blog post.

Top comments (0)