How to update a collection using Eloquent Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Updating Collections Using Eloquent Laravel with MySQL Queries
Introduction: The efficiency of your application depends on the optimization of different functionalities. One such critical aspect is updating collections in Laravel using Eloquent models. In this post, we'll discuss a better approach to update collection-related data and provide insightful examples for common database usage with MySQL queries.
1. Understanding Collections:
Before diving into the update process, it's crucial to understand collections in Laravel. A collection is an instance of Eloquent objects gathered by a relationship query. In our scenario, we have a one-to-many relationship between Device and Command models, where each device has multiple commands. This relationship can be queried using the commands() method on the Device model:
`$device = Device::find(1);
$commands = $device->commands()->whereStatus("pending")->get();`
2. Updating Collections with save():
The initial code you provided attempts to update a collection of commands using the save() method. Unfortunately, this approach is not entirely correct. As per Eloquent documentation, the save() method cannot be called on collections but can be applied directly on the model objects present within that collection. So, in order to update the status of all pending commands for a specific device, you must iterate through each command and call its update() method.
`foreach ($commands as $command) {
$command->update([
'status' => 'sent',
]);
}`
3. Updating Collections with MySQL Queries:
If you want to update a collection using a raw SQL statement, you can use the DB instance method query() (Laravel 5) or db()->statement() (Laravel 4). Here's an example query to update all pending commands in the database:
`$device = Device::find(1);
$commands = $device->commands();
$query = "UPDATE commands SET status = 'sent' WHERE device_id = ". $device->id . " AND status = 'pending';";
DB::update($query);`
4. A Note on Performance:
While both approaches are valid, updating a collection using the Eloquent models is generally preferred because it provides more control over each command and facilitates better performance. Additionally, it's always recommended to perform this type of update through database transactions (using DB::transaction()) to ensure consistency in case any of the updates fail.
Conclusion:
Updating collections using Eloquent Laravel involves understanding the concept and using the appropriate methods to achieve your desired results. In this comprehensive blog post, we've explored different techniques for updating collections with sample code demonstrating both approaches and practical advice on better performance and consistency. By applying these insights, you can ensure that your Laravel application remains efficient and reliable. And remember, always leverage the right tools for optimized code and robust applications.