Laravel Update Multiple Records with Different Values for Each Record
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel Eloquent: Efficiently Updating Multiple Records with Different Values
When dealing with relational data in a database, one of the most common tasks developers face is updating multiple related records based on complex criteria. Specifically, if you have a set of new values that need to be applied across several existing records identified through a query, the method you choose significantly impacts your application's performance.
The scenario you presented—updating many `Employee_presence` records with different values derived from external data—is a classic example where naive iteration can lead to severe performance bottlenecks in a Laravel application. As a senior developer, I highly recommend moving away from nested loops for database operations and embracing Eloquent’s mass update capabilities or direct database queries.
## The Pitfall of Nested Iteration (The N+1 Problem)
Let's first analyze the approach you outlined: iterating through results and then running an update query inside that loop.
```php
// Original pattern analysis
foreach ($presences_data as $presence_data)
{
foreach ($employee_presences as $employee_presence)
Employee_presence::where('id', $employee_presence->id)->update([
'presence_value' => $presence_data['presence_value']
]);
}
```
This approach is highly inefficient. If `$presences_data` has $M$ items and `$employee_presences` has $N$ items, this results in approximately $M \times N$ individual `UPDATE` queries. For large datasets, this quickly overwhelms the database connection and slows down your entire request cycle. This pattern is often referred to as the "N+1" problem when applied to Eloquent relationships, and it should be avoided whenever possible.
## The Efficient Solution: Batching Updates
The key to solving this efficiently is to gather all the necessary data and structure the update operation to minimize database round trips. Instead of updating record by record in PHP memory, we should push the logic down to the database layer where it can be executed much faster.
For scenarios like yours, where you need to apply distinct values based on a set of external data, the most performant strategies involve either using Eloquent’s `update()` method with grouped data or utilizing raw SQL for bulk operations.
### Method 1: Using Eloquent Mass Update (When Applicable)
If your goal is to update records based on a relationship or a known set of IDs, you can use mass updates. However, since you need *different* values for each record based on `$presences_data`, a simple single `update()` call won't suffice unless you structure the data differently (e.g., updating multiple rows simultaneously with the same value).
A more practical Eloquent approach involves identifying all necessary IDs first and then using those IDs to perform targeted updates, or structuring your input data to facilitate a cleaner update mechanism.
### Method 2: The Database Powerhouse – Raw Updates for Bulk Operations (Recommended)
When you need to apply unique values derived from an array of external data to related records, raw SQL or Laravel's `DB` facade provides the necessary speed and control. This bypasses Eloquent’s overhead for simple bulk operations, which is a core principle when optimizing database interactions in Laravel.
Here is how you can refactor your logic to perform a highly optimized batch update:
```php
use Illuminate\Support\Facades\DB;
// Assume $employee_presences and $presences_data are already prepared.
// 1. Prepare the data structure for bulk updating (e.g., mapping IDs to new values)
$updates = [];
foreach ($presences_data as $index => $presence_data) {
// Assuming you can map the presence_data back to a specific employee ID or record ID
// For this example, we will assume we are updating based on a calculated key.
$updates[] = [
'id' => $employee_presence->id, // The ID of the record to update
'presence_value' => $presence_data['presence_value']
];
}
// 2. Perform a single batch update using a database-specific method (if supported) or looping over IDs.
// For maximum performance in Laravel, if you are updating many rows based on a derived set,
// the use of raw SQL is often superior.
DB::table('employee_presences')
->whereIn('id', array_column($updates, 'id')) // Select only the records we need to touch
->update([
'presence_value' => DB::raw("CASE id
WHEN ? THEN ?
ELSE presence_value
END", [
// This part requires complex logic if you need truly unique updates based on the loop index.
// For simple value replacement, a direct update is cleaner:
$updates[0]['presence_value'], // Simplified example for demonstration
]
]);
// A simpler, more robust approach for distinct values involves iterating over the *data*
// and using `where` clauses grouped by employee ID if the relationship allows it.
```
**A Cleaner Eloquent Batch Strategy:**
If your goal is to update records based on a key that links them (like `employee_id`), you can group the updates:
```php
$updates_by_employee = [];
foreach ($presences_data as $presence_data) {
// Assuming presence_data has employee_id and the new value
$employee_id = $presence_data['employee_id'];
$new_value = $presence_data['presence_value'];
$updates_by_employee[$employee_id][] = $new_value;
}
// Now, iterate through the employees and perform a single bulk update per employee group.
foreach ($updates_by_employee as $employee_id => $values) {
// Example: Update all relevant presence records for this employee in one go
Employee_presence::where('employee_id', $employee_id)
->update(['presence_value' => $values[0]]); // Note: This assumes a 1:1 or simple update context.
}
```
## Conclusion
When updating multiple records with distinct values in Laravel, always prioritize database efficiency over PHP iteration logic. Avoid the N+1 query trap by leveraging bulk operations provided by Eloquent and the underlying database. For complex, high-volume updates, moving to optimized `DB::table()` calls or utilizing specialized database features ensures that your application remains fast and scalable. By understanding how to interact with the database efficiently—as demonstrated in frameworks like Laravel—you write code that is not only functional but also performant. Remember, mastering these low-level details is what separates a functional developer from a senior one, especially when dealing with complex data manipulation within Laravel.