PHP code example of itsimiro / laravel-chunk-cursor

1. Go to this page and download the library: Download itsimiro/laravel-chunk-cursor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

itsimiro / laravel-chunk-cursor example snippets


use App\Models\User;

User::query()->with(['articles', 'friends'])->cursor()->each(function (User $users) {
    // Process user... But 'articles' and 'friend' won't be loaded here :(
});

use App\Models\User;

User::query()->with(['articles', 'friends'])->chunkCursor(50)->each(function (Collection $users) {
    foreach ($users as $user) {
        // Process user... With eager loading! :)
    }
});

$startCursor = microtime(true);

DatabaseChunkCursorTestUser::query()->cursor()->each(function (DatabaseChunkCursorTestUser $user) use (&$i) {
    $this->assertEquals(true, true);
});

$endCursor = microtime(true);

$timeCursor = number_format($endCursor - $startCursor, 6); // 0.006624

$startChunkCursor = microtime(true);

// 1000 rows
DatabaseChunkCursorTestUser::query()->chunkCursor(10)->each(function (Collection $users) use (&$i) {
    $i += 10;
    $this->assertLessThanOrEqual(10, $users->count());
});

$endChunkCursor = microtime(true);

$timeChunkCursor = number_format($endChunkCursor - $startChunkCursor, 6); // 0.003406