Performance: Model events and observers
MEDIUM IMPACT
This affects the server-side response time and indirectly impacts client perceived speed by adding processing before or after database operations.
class UserObserver { public function created(User $user) { Log::info('User created: ' . $user->id); Notification::send($user, new WelcomeNotification()); } } // In AppServiceProvider boot method User::observe(UserObserver::class);
public function store(Request $request) {
$user = User::create($request->all());
// Logging directly in controller
Log::info('User created: ' . $user->id);
// Sending notification directly
Notification::send($user, new WelcomeNotification());
return redirect()->back();
}| Pattern | Server Processing | Database Calls | Response Time Impact | Verdict |
|---|---|---|---|---|
| Side effects in controller | High (mixed logic) | Normal | Increases by 50-100ms | [X] Bad |
| Using model observers | Moderate (centralized) | Normal | Minimal if synchronous | [!] OK |
| Observers with queued jobs | Low (async) | Normal | Minimal, non-blocking | [OK] Good |