Eloquent model mass update

Eloquent model mass update

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Mass Updates in Eloquent Without Query Bloat

The short answer to the question of whether Eloquent supports mass updates is a definitive yes, but it doesn't come as a static method like User::updateWhere(). The confusion often stems from how Laravel encourages developers to interact with models versus raw database tables. While the desire for a fluent API that accepts a condition and an update array—similar to what you sketched out—is understandable, the existing solution relies on chaining query scopes directly onto your model class.

The core issue you identified regarding table name refactoring is valid. If you use DB::table('users')->where(...), you indeed couple your logic to a specific string literal. This makes future schema changes painful, requiring global search-and-replace operations that are prone to human error. Eloquent was designed specifically to decouple the database layer from the application logic by binding queries to the model class itself.

To perform a mass update without issuing a query for every single row, you leverage the underlying update method on the Eloquent collection or directly on the Model class when scoped correctly. The most idiomatic approach that respects your constraint against raw table names is:

User::where('age', '<', 18)->update(['under_18' => 1]);

This single statement generates a single SQL UPDATE query. It bypasses the Model's mass assignment protection for the columns you are updating, but it still respects your model's fillable properties if you wrap the logic carefully or use $guarded. However, there is a critical nuance here: this method does not trigger any event listeners attached to the model (like updating or updated) and does not cast attributes. It is purely a database operation executed against the underlying table.

If you require full Eloquent lifecycle management—such as triggering events, casting dates to the correct format, or validating fillable columns—you cannot use this direct approach. In that scenario, you must iterate over a collection using chunk, which processes rows in batches while maintaining model integrity:

User::where('age', '<', 18)->chunk(100, function ($users) {
    foreach ($users as $user) {
        $user->update(['under_18' => 1]);
    }
});

While this iterates in loops, the chunk method ensures you are not loading thousands of rows into memory at once, preventing memory exhaustion. However, be aware that every iteration here still fires model events. If your goal is strictly performance for a bulk operation where events don't matter, the direct update call is superior.

Regarding extending Illuminate\Database\Eloquent\Model to add a custom static method like updateWhere, technically you could do so by defining a class method that internally calls the query builder. However, this often leads to code duplication and bypasses the elegance of Laravel's scope system. Instead of creating custom static methods for common patterns, define a scope:

public function scopeUnderEighteen($query)
{
    return $query->where('age', '<', 18);
}

Then your usage becomes User::underEighteen()->update(['under_18' => 1]);. This keeps the logic within the model, ensuring that if you ever change the table name or add a new column to the fillable list, the behavior adapts automatically without needing to refactor static methods scattered across controllers.

For teams looking to standardize these patterns further, resources like https://laravelcompany.com often discuss how to balance strict Eloquent conventions with raw performance needs when scaling applications. The takeaway is that while a dedicated static method might seem convenient, leveraging scopes and understanding the difference between mass assignment updates and direct SQL execution provides a more robust foundation for your application architecture.

Our network