Lazy Collection is a class offering generators for the array objects. You can notice the set class used for wrapping array objects when you encounter Laravel 5.x edition. Also, essentially, we have a new collection named Lazy Collection and a Regular Collection Class in Laravel 6.
Route of class collections - project_folder\vendor\laravel\framework\src\illuminate\supports\collection.php class of Lazy collections - project_folder\vendor\laravel\framework\src\illuminate\supports\lazycollection.php |
Use of Class Lazy Collections
The lazy class of sets is primarily built to maintain Laravel applications’ memory low. It utilizes the capacity of PHP generators which helps one to use low memory in broad databases.
Let’s say that there is a ton of data in your application storage table and that you want to have certain documents OR to Excel OR some file for your website necessity.
Only assume if your program uses Laravel Eloquent’s all() form, then you may be confronted with a misplacement in your memory and it is because we recover all records in one direction. You would be unable to qualify for some period if you do this.
Laravel 6 has several really powerful features that enable website users with poor memory to work smoothly.
Cursor method Retrieve Records
Customization method Laravel Lazy Collection helps one to get no records from the models provided and then returns the item Lazy Collection. Looking at the illustration below, you can see that all models from the Lazy Selection Object are produced easily.
$employees = \App\employees::cursor(); foreach($employees as $key => $value) { echo $value->name; echo "<br>"; } |
In the example above, SQL query will be performed only when any loop starts to run. Below is the definition of a cursor system in which the generator output is used.
In the code below, you will find the description of the cursor form.
public function cursor() { if (is_null($this->columns)) { $this->columns = ['*']; } return new lazycollection(function(){ yield from $this->connection->cursor( $this->toSql(), $this->getBindings(), ! $this->useWritePdo ); }); } |
How to sort lazy selection records:
Set of Lazy the cursor solution has filtering functionality and Laravel Architecture offers one of the many advantages. For instance, by using the cursor filter tool, if you want to filter any unique records from thousands of rows.
An example of Lazy’s set type filtering records can be found here:
$students = \App\employees::cursor()->filter(function ($employees) { return $employees->id > 5000; }); foreach ($employees as $key => $value) { echo $key . "--" . $value->name; } |
In the above example, you see that we have 5,000 filter documents, so after 5,000 lines we get them.
How to interpret the log file with the class Lazy Collection:
You may link to the code below if you want to read some file or claim a log file with the help of the Lazy collection class. We read log files that have minimal machine memory consumption, please notice.
Take a 60,000 logfile lines example. To use these class protocols, initializing the lazy class array first will be necessary.
use illuminate\supports\lazycollection; $filelogs = lazycollection::make(function () { $filehandle = fopen('./logfile.txt', 'r'); while (($fileline = fgets($filehandle)) !== false) { yield $fileline; } }); foreach ($filelogs as $fileline) { echo $fileline . '<br>'; } |
Looking at this case, you can see that we named a lazy collection class to read a log file to read each line and print that line. The aim is to read large log files without application in a faint selection class. The app goes down when you read a big log file. While we use an array instead of yield.
Lazy Collection Class Methods
Lazy collection class may access all collection class methods, and tap each() method has moreover lazy collection class. Collection class is a related approach but with an important change.
In the default collection type, by using a callback function, it will automatically call each element in the collect object so when you ask the callback function to take it out of the specified array, then tap each() method will call the element once.
use illuminate\supports\lazycollection; Route::get('/', function () { $lazycollection = lazycollection::times(INF)->tapEach(function ($value) { dump($value); }); $array = $lazycollection->take(100)->all(); dump($array); }); |
This indicates that we call 100 in the set, but if you comment line $array = $lazyCollection-> take(100)->all() then nothing’s finished or dumped yet. When we call the method for executing else it will return null, the Lazy list class takes tables data.
Conclusion
We are fairly confident that many Laravel apps will be able to utilize the Lazy selection class and we hope that this blog will certainly help to obtain information and speed up the submission. Feel free to contact us regarding Laravel development.