Update multiple rows of database in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Updating Multiple Rows of Database in Laravel Introduction When working with databases in Laravel, it's crucial to have efficient methods for maintaining data integrity. While performing updates on individual records might seem straightforward, handling bulk updates can be more complex and error-prone if not done correctly. In this blog post, we'll discuss a better way to update multiple rows using the power of Laravel's ORM (Object Relational Mapping) functionality. Why Not Use Raw SQL Queries? Before diving into the solution, it is essential to understand why raw SQL queries aren't the best approach when updating multiple database rows. Firstly, they might lead to errors if the data structure or table names change unexpectedly. Secondly, writing and maintaining raw SQL queries can be time-consuming and tedious. Last but not least, raw SQL queries require manual iteration over the collection of rows, leading to a more complex code base. A Better Way with Laravel ORM Fortunately, Laravel's Eloquent model provides an elegant solution for updating multiple rows simultaneously. Here's how you can update multiple records using Laravel: 1. Define your model class and migration - Create a new model called "Value" that extends the Eloquent model. - Ensure a migration is created, including the table schema with the necessary columns such as `project_id`, `data`, etc. 2. Implement update logic - Retrieve all the values by filtering based on the project ID. You can use the `whereHas()` method or create scope methods to make your code more readable and maintainable. - Iterate over the collection of values with a foreach loop, updating the data based on the new default value for each record. - Use the updated data in JSON format and set it using the Eloquent model's `update()` method. 3. Save all updates - After completing the update for each value, save the records to the database using the `save()` function. If you need to retrieve any errors during this process, you can use the `flushErrorOutput()` method. Implementation Example:
$values = Value::where('project_id', $id)->get();
$data = [
  ["default" => "male"], // Assuming all default values are 'male' for this example
];
foreach ($values as $value) {
  // Update data with new default value for each record
  $value->update(['data' => json_encode($data[0])]);
}

// Save updated records to the database
$values->save();
Conclusion By following these steps, you can efficiently update multiple rows of a database in Laravel. This method ensures your code is more maintainable, readable, and secure as it utilizes Laravel's ORM functionality for data management. Be sure to always use appropriate validations and transactions while working with sensitive data to avoid any potential issues.