Skip Model Events in Laravel Using withoutEvents()
The Problem: Unwanted Event Triggers
In Laravel applications, model events are powerful:
creatingupdatingdeleted
But sometimes…
👉 You don’t want them to run.
Real Issue Example
Imagine:
- You update 10,000 users
- Each update triggers observers
- Emails, logs, jobs are fired
👉 System slows down or breaks.
🔥 The Solution: withoutEvents()
Laravel provides a way to temporarily disable all model events.
Example
use Illuminate\Database\Eloquent\Model;
Model::withoutEvents(function () {
User::where('status', 'inactive')->update([
'status' => 'active'
]);
});
⚡ What Happens
- No observers triggered
- No events fired
- Only database operation runs
🔹 Real Project Example
Bulk data correction:
Model::withoutEvents(function () {
Order::query()->update([
'processed' => true
]);
});
🎯 Why This Is Useful
It helps you:
- Avoid unnecessary event execution
- Improve performance
- Prevent unwanted side effects
- Run system-level operations safely
🧠 When to Use It
Use it when:
- Running bulk updates
- Migrating or fixing data
- Importing large datasets
- Avoiding observers temporarily
❌ When NOT to Use It
Avoid when:
- Business logic depends on events
- You need observers to run