Home >Backend Development >PHP Tutorial >How and When to Use saveQuietly() for Silent Updates in Laravel
In Laravel, saveQuietly() is a method available on Eloquent models that allows you to save a model without firing any events, such as creating, created, updating, updated, and other Eloquent model events. This can be useful in situations where you want to update or save data without triggering any additional actions tied to those events, such as logging, notifications, or data validation.
Here’s a step-by-step guide with a hands-on example of saveQuietly() in Laravel, including a detailed explanation of each part.
Imagine you have a User model, and every time a user is updated, an event triggers that sends a notification to the user. However, in some specific cases (such as admin updates or background maintenance tasks), you may want to update user information silently without triggering this notification.
In your User model, you may have event listeners for updating and updated events, which get fired when a user is updated.
Example User model with events:
namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $fillable = ['name', 'email', 'status']; protected static function booted() { // Event listener for updating static::updating(function ($user) { // Log or handle the update event \Log::info("User is being updated: {$user->id}"); }); // Event listener for updated static::updated(function ($user) { // Example action, such as sending a notification $user->notify(new \App\Notifications\UserUpdatedNotification()); }); } }
Here, each time a user is updated:
When you update a user using save(), these events will fire.
Example:
$user = User::find(1); $user->status = 'active'; $user->save();
Expected Result: The updating and updated events are triggered, which means the log entry will be created, and the user will be notified.
To avoid triggering these events (e.g., if an admin updates the user status as part of a bulk operation), you can use saveQuietly().
Example:
$user = User::find(1); $user->status = 'inactive'; $user->saveQuietly();
With saveQuietly(), neither the updating nor updated events are fired, meaning:
namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $fillable = ['name', 'email', 'status']; protected static function booted() { // Event listener for updating static::updating(function ($user) { // Log or handle the update event \Log::info("User is being updated: {$user->id}"); }); // Event listener for updated static::updated(function ($user) { // Example action, such as sending a notification $user->notify(new \App\Notifications\UserUpdatedNotification()); }); } }
$user = User::find(1); $user->status = 'active'; $user->save();
$user = User::find(1); $user->status = 'inactive'; $user->saveQuietly();
saveQuietly() is beneficial in scenarios like:
Here’s how you might put this into a Laravel controller to handle admin updates:
$user = User::find(1);
Using saveQuietly() can significantly streamline tasks where event handling is unnecessary, giving you greater control over Eloquent model behavior in Laravel.
The above is the detailed content of How and When to Use saveQuietly() for Silent Updates in Laravel. For more information, please follow other related articles on the PHP Chinese website!