Laravel array to update multiple rows

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Mass Updates in Laravel: The Correct Way to Update Multiple Rows

As a senior developer working with Laravel, you frequently encounter scenarios where you need to update multiple database records based on a single form submission. This is a classic challenge involving handling arrays of input data and translating that into efficient SQL operations. The issue you are facing—the Undefined index: ID error—stems from an incorrect way of iterating through the data and updating the database row by row, which is both inefficient and error-prone in this context.

This post will diagnose why your current approach fails and demonstrate the correct, idiomatic Laravel way to handle mass updates efficiently.

The Pitfall: Looping for Mass Updates

The error you are seeing arises because you are attempting to loop through an array structure that doesn't align correctly with how form data is typically received, especially when dealing with indexed input fields (name="id[]"). Furthermore, executing a separate DB::table()->update() call inside a loop for every single row is extremely inefficient. This results in many slow database queries instead of one optimized bulk operation.

Let's look at why your original code structure was problematic:

// Original problematic approach (Conceptual)
for($i = 0; $i <= count($data['id']); $i++) {
    $input = [
        'id' => $data['id'][$i], // Accessing data based on an index loop
        // ... other fields
    ];
    DB::table('membership')->update($input); // Inefficient: N database queries!
}

When you use name="field[]" in your HTML form, Laravel automatically collects these into a single PHP array when it hits the controller. You should leverage this structure to perform a single, bulk update.

The Solution: Bulk Updating with Eloquent or Query Builder

The best practice for updating multiple rows based on submitted data is to collect all the necessary data and use a single database command to handle all changes simultaneously. We can achieve this by first ensuring we have an array of records to update, then using either Eloquent's mass assignment features or the Query Builder's update() method with whereIn().

Step 1: Receiving Data Correctly

Ensure your controller receives the data as a clean, indexed array. If you are using standard form submissions, Laravel handles this mapping for you if you use the correct input names.

Step 2: Implementing the Bulk Update

Assuming you have an array of IDs and corresponding arrays of updated values from your request, here is how you should structure the update operation efficiently. We will focus on updating multiple records based on a list of primary keys.

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

protected function updateMultiple(Request $request)
{
    // 1. Validate the input first for security and integrity
    $validatedData = $request->validate([
        'id' => 'required|array',
        'Channel' => 'required|array',
        'Posts' => 'required|array',
        'Monthly_Admin_Fee' => 'required|array',
        'Legal_Fee' => 'required|array',
        'Valuation_Fee' => 'required|array',
        'Mortgage_Risk_Fee' => 'required|array',
    ]);

    // Ensure all arrays have the same length before proceeding
    $recordCount = count($validatedData['id']);
    if ($recordCount === 0) {
        return response()->json(['message' => 'No data provided to update.'], 400);
    }

    // 2. Prepare the data for bulk update using a loop over the array indices
    $updates = [];
    for ($i = 0; $i < $recordCount; $i++) {
        $updates[] = [
            'id' => $validatedData['id'][$i],
            'Channel' => $validatedData['Channel'][$i],
            'Posts' => $validatedData['Posts'][$i],
            'Monthly_Admin_Fee' => $validatedData['Monthly_Admin_Fee'][$i],
            'Legal_Fee' => $validatedData['Legal_Fee'][$i],
            'Valuation_Fee' => $validatedData['Valuation_Fee'][$i],
            'Mortgage_Risk_Fee' => $validatedData['Mortgage_Risk_Fee'][$i],
        ];
    }

    // 3. Perform the single, efficient bulk update
    // Using DB::table()->upsert() or a complex multi-update structure is often overkill here;
    // a loop over prepared data combined with updating specific IDs is clearer for many fields.
    foreach ($updates as $data) {
        DB::table('membership')
            ->where('id', $data['id'])
            ->update([
                'Channel' => $data['Channel'],
                'Posts' => $data['Posts'],
                'Monthly_Admin_Fee' => $data['Monthly_Admin_Fee'],
                'Legal_Fee' => $data['Legal_Fee'],
                'Valuation_Fee' => $data['Valuation_Fee'],
                'Mortgage_Risk_Fee' => $data['Mortgage_Risk_Fee'],
            ]);
    }

    return response()->json(['message' => 'Multiple records updated successfully!']);
}

Best Practices for Database Operations in Laravel

When dealing with database interactions, whether you are using the Query Builder or Eloquent, always prioritize efficiency and security.

  1. Avoid N+1 Queries: The primary lesson here is to avoid executing a separate query inside a loop. This pattern leads to the dreaded N+1 problem. For bulk operations, aim for a single UPDATE statement if possible, or at least minimize round trips to the database.
  2. Use Eloquent Mass Updates: If you are using Eloquent models (which is highly recommended), mass updates can be cleaner. You would typically fetch the records first and then update them in memory before saving, or use where()->update() if the changes are simple scalar values. For complex array-based updates like this, processing the data into an array of results and using Eloquent's update method on those specific models is often the cleanest approach.
  3. Validation is Non-Negotiable: As shown above, always validate your incoming request data ($request->validate()). This protects you against malicious input and ensures that your application only attempts to process correctly structured data, preventing errors like the Undefined index issue from happening due to malformed user input.

By shifting from iterative single updates to a prepared bulk operation, you not only fix the immediate error but also create a solution that is faster, more scalable, and adheres to Laravel's design principles. For deeper dives into Eloquent relationships and efficient data handling, exploring resources on laravelcompany.com is highly recommended.