
In this tutorial I am going to give you examples of using laravel 6 new feature called Lazy collections class.
Lazy Collection is a class that provides an iterators for the items of array, if you know in Laravel 5 we have a Collection class which is used to wrap array items. so basically in Laravel 6 we have additional Lazy Collection class here along with the Normal Collection Class.
Lazy collections is supplement to the collection class.
Learn – To Develop Exclusive Laravel 6 CRUD Application with Input Validations
What is the Use of Lazy Collections Class?
Lazy collection class is specifically designed for keep memory usage low by the laravel applications it usage the power of PHP Generators if you don’t Generator are basically a syntax of creating iterator in PHP.
Let’s imagine that your applications database table has thousands of rows and you want fetch all of the them for a specific requirement may be exporting to excel or any file.
If you try using Laravel Eloquent all()
method most possibly the application will throw run out of memory exception and this is because when you perform all query it is going to fetch all the records and store into the memory so it is obvious that if you try thousands or lakhs or records then the app will blow up.
To overcome or solve this problem and allow application to run smother by utilizing low memory Laravel 6 team has come up with this powerful solution.
Let’s see practical examples of using Laravel lazy collection class
Fetching all Records with Cursor Method:
New query builder’s cursor
method allows us to fetch all the records from the given Model and then it returns a LazyCollection
instance and we can easily iterate all the models from LazyCollection
instance, look the below example:
$posts = App\Post::cursor();
foreach ($posts as $post) {
# code...
}
For the above examples SQL queries are only going to executed when our foreach look will going to start executing.
Filter Records
If you want to filter records from the collection class we can also use the lazy collection, imaging you may have bunch of records to filter from and it is best place to use lazy collection
Here is the example of filtering records using Lazy collection class:
$posts = App\Post::cursor()->filter(function ($post) {
return $post->id > 500;
});
foreach ($posts as $post) {
# code...
}
In the above code filter
callback function is not going execute until we iterate the each post.
Reading Log file using Lazy Collection class
In this example I will show how we can read files using lazy collection class and remember the Goal is to only read the file but the read the file with low memory usage.
First will have to create instance of lazy collection class and then we can move form there.
Quick note – To create lazy collection class object we will have to use make
method along with the PHP generator function.
Here is the example of reading big log file using lazy collection class with the low memory usage
use Illuminate\Support\LazyCollection;
$logs = LazyCollection::make(function () {
$handle = fopen('./log.txt', 'r');
while (($line = fgets($handle)) !== false) {
yield $line;
}
});
foreach ($logs as $line) {
echo $line . '<br>';
}
So in the above example we are making a lazy collection, that will open a file called log.txt read each line and then yield it for the processing, so this is the function which could used to load very large file and it would not let your application down.
So just you understand we are not making any array here while processing the file all we are doing it we reading file line by line inclemently.
Available methods for Lazy collection class
Lazy collection class has access to all the existing method from the Collection
class additionally has tapEach()
method it is similar to each
method from Collection
class but with the useful tweak.
So when you each
method along with the callback function, each method will call each item from the collection instantly but the tapEach
method is only going to call the item when the given callback ask to pull the item from the list, so basically it operators items one by one.
Here is example of using tapEach method:
$lazyCollection = LazyCollection::times(INF)->tapEach(function ($value) {
dump($value);
});
// Nothing has been dumped so far...
$array = $lazyCollection->take(4)->all();
If you comment the $array = $lazyCollection->take(4)->all();
line you will see nothing has been executed of dumped yet it is only going to execute when you ask lazy collection to execute.
Lazy collection class surely going to rock and I am pretty much sure this tutorial will surely help to increase your laravel 6 speed and performance.
If you like this tutorial then don’t forget to share or comment if you get any question specific to the laravel lazy collection class.
Thank youuuu!
TANX