Interview frequency: …
Short answer
Events such as creating, created, updating, and deleted fire during a record's lifecycle; an Observer groups listeners for these events in a dedicated class.
Explanation
Eloquent events can run automatic actions, such as generating a UUID or sending a notification when a record is created, or prevent deletion by returning false from the deleting event. An Observer groups these methods in a clean class and is attached to the model with the #[ObservedBy([UserObserver::class])] Attribute in modern Laravel.
Code example
class UserObserver {
public function creating(User $user): void {
$user->uuid = (string) Str::uuid();
}
}Common mistakes
- Calling $model->save() inside an Observer's updating or saved event without care, causing an Infinite Loop.