Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What are model events in Laravel?
Model events are hooks that Laravel fires at specific points in a model's lifecycle, like creating, updating, or deleting. They let you run code automatically when these actions happen.
Click to reveal answer
beginner
What is an observer in Laravel?
An observer is a class that groups multiple model event handlers. It listens to model events and runs the related methods when those events happen.
Click to reveal answer
intermediate
Name three common model events you can listen to in Laravel.
Common model events include: creating (before a model is saved), updating (before an update), and deleting (before deletion). Others are created, updated, deleted which happen after the action.
Click to reveal answer
intermediate
How do you register an observer for a model in Laravel?
You register an observer in the model's boot method or in a service provider using: Model::observe(ObserverClass::class); This tells Laravel to use that observer for the model's events.
Click to reveal answer
intermediate
Why use observers instead of putting event logic directly in the model?
Observers keep your model code clean and focused on data. They separate event handling logic, making your code easier to read, test, and maintain.
Click to reveal answer
Which Laravel model event fires before a model is saved to the database?
Acreated
Bupdated
Csaved
Dcreating
✗ Incorrect
The 'creating' event fires before a new model is saved to the database.
What method do you use to link an observer class to a model?
AModel::attachObserver()
BModel::listen()
CModel::observe()
DModel::registerObserver()
✗ Incorrect
You use Model::observe(ObserverClass::class) to register an observer.
Which of these is NOT a valid Laravel model event?
Asaving
Bfetching
Cdeleting
Drestoring
✗ Incorrect
'fetching' is not a Laravel model event.
Where is a common place to register model observers?
AIn the model's boot method
BIn the controller constructor
CIn the routes file
DIn the view file
✗ Incorrect
Observers are commonly registered in the model's boot method or a service provider.
What is the main benefit of using observers?
AThey separate event logic from the model
BThey automatically create database tables
CThey replace controllers
DThey speed up database queries
✗ Incorrect
Observers help keep event logic separate from the model, improving code organization.
Explain how Laravel model events and observers work together to handle actions like creating or deleting a model.
Think about how you can run code automatically when a model changes.
You got /4 concepts.
Describe the steps to create and register an observer for a Laravel model.
Focus on creating the class and telling Laravel to use it.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of model events in Laravel?
easy
A. To react automatically when a model is created, updated, or deleted
B. To define database table relationships
C. To create database migrations
D. To write raw SQL queries
Solution
Step 1: Understand model events
Model events are hooks that trigger when a model changes, like creation or deletion.
Step 2: Identify their purpose
They let you run code automatically on these changes, such as logging or notifications.
Final Answer:
To react automatically when a model is created, updated, or deleted -> Option A
Quick Check:
Model events = react on model changes [OK]
Hint: Model events trigger on data changes automatically [OK]
Common Mistakes:
Confusing events with database migrations
Thinking events define relationships
Assuming events run SQL queries directly
2. Which of the following is the correct way to register an observer for a model in Laravel?
easy
A. User::addObserver('UserObserver');
B. User::observe(UserObserver::class);
C. User::registerObserver(UserObserver);
D. User::attachObserver(UserObserver::class);
Solution
Step 1: Recall observer registration syntax
Laravel uses the static method observe() on the model to register observers.
Step 2: Match correct syntax
The correct syntax is Model::observe(ObserverClass::class); which matches User::observe(UserObserver::class);.
Final Answer:
User::observe(UserObserver::class); -> Option B
Quick Check:
Register observer = observe() method [OK]
Hint: Use observe() method with ::class for observers [OK]
Common Mistakes:
Using wrong method names like registerObserver
Passing observer as string instead of ::class
Confusing attachObserver with observe
3. Given this observer method in Laravel:
public function created(User $user) {
Log::info('User created: ' . $user->id);
}
What happens when a new User model is saved?
medium
A. An error is thrown because created() is invalid
B. The user is deleted immediately
C. Nothing happens automatically
D. A log entry with the new user's ID is created
Solution
Step 1: Understand the created() observer method
The created() method runs after a model is saved for the first time.
Step 2: Analyze the method body
It logs info with the new user's ID, so a log entry is made.
Final Answer:
A log entry with the new user's ID is created -> Option D
Quick Check:
created() logs user ID after save [OK]
Hint: created() runs after first save, logs info [OK]
Common Mistakes:
Thinking created() deletes the model
Assuming created() does nothing
Confusing created() with invalid method
4. What is wrong with this observer registration code?
public function boot() {
User::observe();
}
medium
A. The observe() method is called without passing the observer class
B. The boot() method should be named bootObserver()
C. User model cannot have observers
D. The observe() method should be called on the observer class
Solution
Step 1: Check observe() method usage
observe() requires the observer class as an argument to register it.
Step 2: Identify missing argument
Here, observe() is called with no arguments, so it will cause an error.
Final Answer:
The observe() method is called without passing the observer class -> Option A
Quick Check:
observe() needs observer class argument [OK]
Hint: Always pass observer class to observe() [OK]
Common Mistakes:
Calling observe() without arguments
Renaming boot() incorrectly
Calling observe() on wrong class
5. You want to log a message every time a Post model is updated, but only if the title has changed. How should you implement this in a PostObserver?
hard
A. Use the deleted(Post $post) method and check if $post->isDirty('title') is true
B. Use the saving(Post $post) method and check if $post->wasChanged('title') is true
C. Use the updated(Post $post) method and check if $post->wasChanged('title') is true
D. Use the created(Post $post) method and check if $post->title is not empty
Solution
Step 1: Identify correct event for update
The updated() method runs after a model is updated, suitable for this task.
Step 2: Check how to detect changed attributes
wasChanged('title') returns true if the title attribute was changed during the update.
Step 3: Combine logic
Inside updated(), check wasChanged('title') to log only when title changed.
Final Answer:
Use the updated(Post $post) method and check if $post->wasChanged('title') is true -> Option C
Quick Check:
updated() + wasChanged('title') detects title changes [OK]
Hint: Use updated() and wasChanged() to detect attribute changes [OK]
Common Mistakes:
Using created() or deleted() for updates
Using wasChanged() inside saving() which runs before update