For getting every new tutorial link Please join our telegram group

Part 8 laravel eloquent model chunk vs cursor method with hindi video

In this laravel tutorial, we will discuss the chunk and cursor method in an eloquent model with example. what is the difference between chunk and cursor method with hindi video.

Laravel eloquent chunk vs cursor method

  1. chunk()
  2. cursor()

Laravel eloquent chunk() method

When our website process more than thousand of data then server memory out and data not process. Laravel chunk method retrieved a bit of data. The chunk method returns only some data at a time. Thus chunk method reduces the memory uses and working with thousands of data. Let an example of chunk method

Chunk method first argument pass number of records that are received once than again next same number of records received this process run until all data, not process. Let an example of chunk method

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller{
 public function updateCreate(){
    Student::chunk(200, function ($students) {
		    foreach ($students as $student) {
		        //do some data update operation here
		    }
		});

  }

}

Laravel eloquent cursor() method

Laravel cursor method also works on memory reduction and operates on thousands of data. The cursor method greatly reduces your memory consumption. When processing a large amount of data than cursor method use. The cursor method runs a single query for large data. This method paginate. Chunk method easily paginates your query. Let an example of the cursor method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller{
 public function updateCreate(){
    $students = Student::cursor()->filter(function ($student) {
    	return $student->id > 500;
		});

		foreach ($students as $student) {
		    echo $student->id;
		}

  }

}

You read this tutorial on advanced web tutorial. here we provide laravel beginners to advanced laravel tutorial.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links