To use, add the trait to your project and then add the CanSaveQuietly
trait to your model:
Make sure to change the namespace of the trait to match your project's structure!
class Product extends Model
{
use CanSaveQuietly;
// ...
}
Then you can save or update your model instance without firing any model events:
/**
* @param OrderDeskItem $item
* @param Product $product
*/
protected function associateItemToProduct(OrderDeskItem $item, Product $product): void
{
$product->updateQuietly([
'order_desk_inventory_item_id' => $item->id,
'order_desk_inventory_item_assigned_at' => now(),
'order_desk_inventory_item_updated_at' => now(),
]);
}
This is particularly useful in situations where you have a model event listener that results in the model being saved/updated. If you allow model events to fire then you'll end up in an endless loop.
For example, in EventServiceProvider
:
protected $listen = [
ProductUpdated::class => [
UpdateItemOnOrderDesk::class,
],
];
And in the UpdateItemOnOrderDesk
listener:
$product->update([
'order_desk_inventory_item_id' => $item->id,
]);
Doing this would cause the ProductUpdated
event to fire, which fires the UpdateItemOnOrderDesk
listener, which calls $product->update()
again, which fires the ProductUpdated
event again, and so on.
By instead doing it this way:
$product->updateSilently([
'order_desk_inventory_item_id' => $item->id,
]);
You avoid causing the ProductUpdated
event to fire again.