Laravel Eloquent update just if changes have been made
Laravel does not provide a built-in method on the Model class to silently skip an update operation if no attributes have actually changed since the last query. The framework treats every call to $model->save() as a potential write operation, meaning it will generate a SQL UPDATE statement regardless of whether the data differs from what is stored in the database. If you rely solely on Eloquent's internal logic to detect "no changes," you will inevitably hit the database unnecessarily, which impacts performance and violates your goal of minimizing unnecessary writes.
To implement server-side detection for changed attributes, you must explicitly compare the current state of the model against its original state from the database. The most robust way to do this in Eloquent is by utilizing the original property or the getOriginal() method. When you instantiate a model using find(), Laravel populates an internal array with the values retrieved from the database. This snapshot remains unchanged until the model is modified or explicitly refreshed.
Here is how you can refactor your existing code to conditionally execute the save:
$product = Product::findOrFail($data["id"]);
// Check if any attribute differs from the original database record
if ($product->isDirty()) {
// Only proceed with saving if changes exist
$product->title = $data["title"];
$product->description = $data["description"];
$product->price = $data["price"];
// Perform the update
$product->save();
}
The isDirty() method returns a boolean indicating whether any of the model's attributes differ from their original values. This is an internal Eloquent mechanism that tracks changes efficiently without requiring you to manually compare every single column. By wrapping your assignment logic and the subsequent save() call inside this conditional check, you ensure that the database write only occurs when necessary.
However, there are important nuances to consider regarding how Eloquent tracks dirty attributes. The isDirty() method checks against the values present in the model's internal $attributes array at the time of initialization. If you modify a model after fetching it but before checking isDirty(), that modification will be detected. Conversely, if you fetch a model, assign new values immediately, and then check, the comparison is accurate.
One common pitfall involves mass assignment and the original property reset. When you use methods like $product->fill($data) or $product->syncWithModel(), Eloquent updates the current attributes but also updates the original snapshot to match the new values if they are identical. If your form submission contains empty strings for fields that were previously populated with data, isDirty() will correctly identify this as a change, triggering an update. However, if you are using Laravel 10 or higher with strict mass assignment rules enabled in older versions or specific configurations, ensure that the $fillable property on your model is correctly defined to match the columns you intend to update.
If you need more granular control, such as updating only specific columns regardless of whether they changed, you might consider using update() instead of save(). The update() method accepts an array of attributes and executes a raw SQL update statement. It does not track dirty attributes in the same way Eloquent models do; it simply writes whatever is passed to it. This can be useful if you want to perform an "upsert" logic or handle partial updates without loading the entire model into memory, though for your specific use case of preventing any write if nothing changed, the isDirty() approach remains the cleanest solution within the Eloquent paradigm.
For developers who need to implement this pattern across many controllers, consider extracting the check into a service class or a middleware component that accepts the model and a data payload. This keeps your controller code focused on validation and response handling rather than database interaction logic. If you are looking for further patterns on optimizing database interactions in Laravel, resources like https://laravelcompany.com often discuss performance tuning strategies that complement these architectural decisions.
