Laravel Bulk UPDATE
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Laravel Bulk Updates: Generating Random Data Efficiently
As a senior developer working with large datasets, you inevitably run into performance bottlenecks when trying to perform operations on millions of records. The scenario you described—updating a column with unique, randomly generated values for every row—is a classic example where iterating through models and executing individual database queries becomes an absolute performance killer.
If you are dealing with 2 million records, looping in PHP and sending 2 million separate UPDATE statements will inevitably lead to timeouts and massive overhead. The solution lies not just in clever chaining of Eloquent methods, but in leveraging the power of raw SQL for true bulk operations.
This post will explore why your initial approach was slow, and provide the most efficient, developer-focused strategies for performing large-scale bulk updates in Laravel.
The Pitfall of Looping: Why Individual Updates Fail
Let's look at the typical, inefficient way developers first attempt this task:
// Inefficient Method Example (Avoid this for large datasets)
$vouchers = Voucher::all(); // This pulls all 2 million records into memory!
$sql_statements = [];
foreach ($vouchers as $voucher) {
// Generating random data in PHP and building a query string
$randomSlug = Str::random(32);
$sql_statements[] = "UPDATE vouchers SET slug = '" . $randomSlug . "' WHERE id = " . $voucher->id . ";";
}
// Executing the statements one by one is slow!
DB::statement(implode(' ', $sql_statements)); // Still potentially problematic depending on driver limits.
The fundamental issue here is twofold: first, fetching all records into PHP memory (Voucher::all()) is memory-intensive and slow for millions of rows; second, executing thousands of separate queries forces the database to manage thousands of individual transaction setups, which significantly increases latency compared to a single, optimized operation.
The Bulk Solution: Leveraging Database Power
To achieve high performance when updating large sets of records, we must push the heavy lifting down to the database engine. Instead of generating the updates in PHP and sending them one by one, we aim to generate the data or structure the update command within a single query.
Strategy 1: Using DB::raw for Bulk Updates (The Direct Approach)
When you need to assign values based on calculations or generated strings across many rows, using DB::raw() allows you to execute a single SQL statement that handles the entire operation. For generating random slugs, this often involves leveraging database-specific functions if available, or relying on methods that generate data internally.
Since Laravel doesn't have a native "generate random string per row" function directly in Eloquent for this specific scenario across all databases, the most practical bulk approach is often to use a technique that minimizes PHP interaction while still achieving randomness. If your database supports functions like RAND() or sequences, you can generate the values directly.
If we stick to generating unique strings via PHP (which is common), the optimized approach involves batching data retrieval and updating in larger chunks rather than individual rows. However, for truly random slugs, a hybrid approach often works best: retrieve IDs and use chunked updates.
Here is a conceptual example focusing on batching operations:
use Illuminate\Support\Facades\DB;
function bulkUpdateSlugs(int $batchSize = 10000)
{
// Step 1: Select the IDs you need to update in batches
$ids = DB::table('vouchers')->pluck('id');
// Step 2: Iterate over the IDs in manageable chunks
foreach (array_chunk($ids->toArray(), $batchSize) as $batchIds) {
// Generate slugs for this batch in PHP
$updates = [];
foreach ($batchIds as $id) {
// Use a cryptographically secure random string generator
$updates[] = [
'id' => $id,
'slug' => Str::random(32) . '-' . $id // Example unique slug generation
];
}
// Step 3: Perform a single, bulk update for the batch using INSERT/UPDATE logic if possible,
// or execute batched updates. For simplicity and safety with random data, we use raw statements within the loop structure:
$sql = "UPDATE vouchers SET slug = ? WHERE id IN (" . implode(',', $batchIds) . ")";
// Note: In a high-volume scenario, executing this update via raw queries is significantly faster than looping individual executions.
DB::statement($sql, [$updates[0]['slug']]); // Simplified for demonstration; real implementation needs careful parameter binding.
}
}
// bulkUpdateSlugs(5000);
Conclusion: Performance Through Batching and Design
When tackling bulk operations in Laravel, remember the golden rule: Minimize database round trips. Never iterate over millions of models and execute individual queries within that loop if you can avoid it.
For your specific requirement—generating random slugs—the most robust solution involves batching the IDs you need to update (e.g., 10,000 at a time) and executing a single, composite UPDATE ... WHERE IN (...) statement per batch. This reduces overhead dramatically compared to running millions of single-row updates.
Always strive for database-centric solutions where possible. For more advanced architectural patterns that manage complex data transformations efficiently, exploring concepts found in modern frameworks like those promoted by Laravel Company can provide deeper insights into optimizing your application's performance.