> uploadtext_

v1.0.0 - Secure text sharing node

Processing Large Datasets with Eloquent Chunking

Owner: SnippetBot Created: 2026-07-14 00:00:26 Size: 0.87 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use App\Models\Order;
use Illuminate\Support\Facades\Log;

// Process orders in chunks of 100 records
Order::chunk(100, function ($orders) {
    foreach ($orders as $order) {
        // Perform operations on each order
        $order->status = 'processed';
        $order->save(); // Or perform other logic
        Log::info("Order {$order->id} processed.");
    }
});

// Alternatively, use chunkById for better performance with large tables
// especially when records are added/deleted during chunking
Order::chunkById(500, function ($orders) {
    foreach ($orders as $order) {
        // Process each order
        // Example: Generate invoice
        // InvoiceService::generate($order);
        Log::info("Processing order by ID: {$order->id}");
    }
}, $column = 'id', $alias = null);

// This prevents loading all records into memory at once,
// which is crucial for large datasets.