Challenge - 5 Problems
Laravel Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a Laravel event listener is triggered?
Consider a Laravel event listener that listens to a UserRegistered event. What is the expected behavior when the event is fired?
Laravel
class SendWelcomeEmail { public function handle(UserRegistered $event) { Mail::to($event->user->email)->send(new WelcomeEmail()); } } // Event fired somewhere in the app event(new UserRegistered($user));
Attempts:
2 left
💡 Hint
Think about what the listener's handle method is doing with the event data.
✗ Incorrect
The listener's handle method uses the event's user data to send a welcome email. It does not perform any other actions like updating password, deleting user, or logging out.
📝 Syntax
intermediate2:00remaining
Which code correctly registers a listener in Laravel's EventServiceProvider?
You want to register a listener named SendWelcomeEmail for the UserRegistered event. Which code snippet correctly does this in the EventServiceProvider?
Attempts:
2 left
💡 Hint
Check the property name and the array structure for event to listeners mapping.
✗ Incorrect
The correct property is $listen, mapping event class names to arrays of listener class names. Option B follows Laravel's standard syntax.
🔧 Debug
advanced2:00remaining
Why does this Laravel listener not respond to the event?
Given this listener code, why does it not respond when the event is fired?
class LogUserLogin {
public function handle(UserLoggedIn $event) {
Log::info('User logged in: ' . $event->user->id);
}
}
// EventServiceProvider
protected $listen = [
UserLogin::class => [LogUserLogin::class],
];
Attempts:
2 left
💡 Hint
Check the event class names used in the listener and the provider.
✗ Incorrect
The event fired is UserLoggedIn but the provider listens for UserLogin, so the listener never triggers.
❓ state_output
advanced2:00remaining
What is the output when multiple listeners modify the same event data?
Consider an event OrderPlaced with a property $order->status initially 'pending'. Two listeners modify this status:
Listener1: sets status to 'processing'
Listener2: sets status to 'completed'
If both listeners are registered and the event is fired, what will be the final status of the order?
Attempts:
2 left
💡 Hint
Think about how listeners run and modify shared event data.
✗ Incorrect
Listeners run in order and can modify event data. The last listener's change will be the final state.
🧠 Conceptual
expert2:00remaining
Which statement about Laravel event listeners is true?
Select the correct statement about Laravel event listeners.
Attempts:
2 left
💡 Hint
Think about how Laravel handles long-running or delayed listener tasks.
✗ Incorrect
Listeners that implement ShouldQueue are run asynchronously via Laravel's queue system. Manual registration is usually required but not always (e.g., auto-discovery). Listeners do receive event data. Automatic retries require explicit configuration.