How to set every row to the same value with Laravel's Eloquent/Fluent?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Updating Multiple Rows with Laravel's Eloquent/Fluent - A Comprehensive Guide Introduction Setting the same value for a particular field in multiple rows of a database table is a common operation in application development. In this article, we'll explore different ways to update these fields using Laravel's Eloquent and Fluent query builders. We will start by discussing the problem with conventional methods, followed by a better approach using Mass Assignment. Lastly, we will look into updating multiple rows with a single statement using the Eloquent bulk update method. Problem with Conventional Updates When you want to set a specific field value for all rows in your database table, there are two conventional ways of doing this: 1. Run an individual query for every row. 2. Fetch all the records using one or multiple queries and then iterate through them manually, updating each row with the desired value. These approaches can be slow, especially if you have a large number of rows in your table. Furthermore, they require more code lines to execute. Better Approach: Mass Assignment Mass assignment can solve this problem by allowing us to update multiple fields on an object or collection all at once. Here's how it works with Laravel Eloquent: 1. Fetch the rows you want to update via a query. 2. Apply mass assignment by merging your data array with the fetched records. 3. Save the updated records using the 'updateOrCreate' method or any other suitable mass assignment methods. Using this approach, we can reduce the number of code lines needed and improve performance for large datasets. Efficient Update Method: Eloquent Bulk Updates Laravel Eloquent provides a bulk update functionality that allows you to update multiple rows with a single statement. Follow these steps to efficiently update all rows in your table: 1. Define the conditions under which you want to perform the update, such as specific field values or other criteria. 2. Update the desired fields using Laravel's 'update' method on the query builder. 3. Execute the bulk update with the defined conditions. For our example table: 1. Define the conditions where we want to set the confirmed field to 1: $confirmedRows = MyModel::where('confirm', '=', '0')->get(); 2. Update the confirmed field for all the rows found using mass assignment or the update method: $confirmedRows->update(['confirmed' => 1]);
Note that in this case, you can also use `$modelInstance->update()` if you have an existing object instead of a collection. 3. Execute the bulk update with the defined conditions using the 'where' statement: MyModel::where('confirm', '=', '0')->update(['confirmed' => 1]); Conclusion In this comprehensive guide, we explored multiple ways to update a field in all rows of a database table with Laravel's Eloquent and Fluent query builders. While the conventional methods may work, they can be slow and cumbersome for large datasets. Using mass assignment or the bulk update functionality provides more efficient and streamlined approaches that will save time and effort. By following these techniques, you can ensure your database remains up-to-date with minimal code and optimal performance.