PHP code example of yogameleniawan / realtime-job-batching

1. Go to this page and download the library: Download yogameleniawan/realtime-job-batching 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/ */

    

yogameleniawan / realtime-job-batching example snippets




namespace App\Repositories;

use Illuminate\Support\Collection;
use YogaMeleniawan\JobBatchingWithRealtimeProgress\Interfaces\RealtimeJobBatchInterface;

class VerificationRepository implements RealtimeJobBatchInterface
{
    /**
     * Get all data to be processed
     * @return Collection
     */
    public function get_all(): Collection
    {
        // Return collection of data to be processed
        return collect([
            // Your data here
        ]);
    }

    /**
     * Process individual data item
     * @param mixed $data
     * @return void
     */
    public function save($data): void
    {
        // Your business logic here
        // Update/delete/process the data
    }
}



namespace App\Http\Controllers;

use YogaMeleniawan\JobBatchingWithRealtimeProgress\RealtimeJobBatch;
use App\Repositories\VerificationRepository;

class BatchController extends Controller
{
    public function executeVerification()
    {
        $result = RealtimeJobBatch::setRepository(new VerificationRepository())
                                 ->execute(name: 'User Verification Process');
        
        return response()->json([
            'message' => 'Batch job started successfully',
            'batch_id' => $result->id
        ]);
    }
}



namespace App\Repositories;

use Illuminate\Support\Collection;
use YogaMeleniawan\JobBatchingWithRealtimeProgress\Interfaces\RealtimeJobBatchInterface;

class VerificationRepository implements RealtimeJobBatchInterface
{
    /**
     * Dapatkan semua data yang akan diproses
     * @return Collection
     */
    public function get_all(): Collection
    {
        // Return collection data yang akan diproses
        return collect([
            // Data Anda di sini
        ]);
    }

    /**
     * Proses item data individual
     * @param mixed $data
     * @return void
     */
    public function save($data): void
    {
        // Logic bisnis Anda di sini
        // Update/delete/proses data
    }
}



namespace App\Http\Controllers;

use YogaMeleniawan\JobBatchingWithRealtimeProgress\RealtimeJobBatch;
use App\Repositories\VerificationRepository;

class BatchController extends Controller
{
    public function executeVerification()
    {
        $result = RealtimeJobBatch::setRepository(new VerificationRepository())
                                 ->execute(name: 'Proses Verifikasi User');
        
        return response()->json([
            'message' => 'Batch job berhasil dimulai',
            'batch_id' => $result->id
        ]);
    }
}
bash
# Create job table
php artisan queue:table

# Create job batches table  
php artisan queue:batches-table

# Run migrations
php artisan migrate
bash
composer 
bash
# Buat tabel job
php artisan queue:table

# Buat tabel job batches
php artisan queue:batches-table

# Jalankan migrasi
php artisan migrate
bash
composer