PHP code example of yovangga / laravel-chunk-process

1. Go to this page and download the library: Download yovangga/laravel-chunk-process 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/ */

    

yovangga / laravel-chunk-process example snippets




namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProsesImporDataJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public $chunk)
    {
    }

    public function handle(): void
    {
        foreach ($this->chunk as $row) {
            // proses setiap baris data di sini
        }
    }
}



namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProsesItemDataJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public mixed $item)
    {
    }

    public function handle(): void
    {
        // proses item di sini
    }
}



namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProsesLangkahPertamaJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public mixed $model)
    {
    }

    public function handle(): void
    {
        // langkah pertama
    }
}

class ProsesLangkahKeduaJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public mixed $model)
    {
    }

    public function handle(): void
    {
        // langkah kedua
    }
}

use App\Jobs\ProsesImporDataJob;
use App\Models\DataMentah;
use LaravelChunkProcess\LaravelChunkProcess\Facades\LaravelChunkProcess;

DataMentah::chunk(1000, function ($chunk) {
    ProsesImporDataJob::dispatch($chunk);
});

use App\Jobs\ProsesImporDataJob;
use App\Models\DataMentah;
use LaravelChunkProcess\LaravelChunkProcess\Facades\LaravelChunkProcess;

LaravelChunkProcess::chunk(DataMentah::class, 1000, function ($chunk) {
    ProsesImporDataJob::dispatch($chunk);
});

use Illuminate\Support\Facades\Bus;
use App\Jobs\ProsesItemDataJob;

$batch = Bus::batch([
    new ProsesItemDataJob($item1),
    new ProsesItemDataJob($item2),
    // ...
])->then(function ($batch) {
    // Semua data selesai diproses.
})->catch(function ($batch, Throwable $e) {
    // Ada job yang gagal.
})->dispatch();

return response()->json(['batch_id' => $batch->id]);

use App\Jobs\ProsesLangkahPertamaJob;
use App\Jobs\ProsesLangkahKeduaJob;
use App\Models\DataMentah;
use LaravelChunkProcess\LaravelChunkProcess\Facades\LaravelChunkProcess;

$model = DataMentah::find($id);

$batch = LaravelChunkProcess::stepsForModel($model, [
    ProsesLangkahPertamaJob::class,
    fn (DataMentah $model) => new ProsesLangkahKeduaJob($model),
])->dispatch();

return response()->json(['batch_id' => $batch->id]);
bash
php artisan vendor:publish --tag="laravel-chunk-process-migrations"
php artisan migrate