0
0
Laravelframework~20 mins

Creating listeners in Laravel - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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));
AThe listener logs the user out immediately.
BThe listener updates the user's password automatically.
CThe listener deletes the user from the database.
DThe listener sends a welcome email to the registered user's email address.
Attempts:
2 left
💡 Hint
Think about what the listener's handle method is doing with the event data.
📝 Syntax
intermediate
2: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?
A
protected $listen = [
    'UserRegistered' => 'SendWelcomeEmail',
];
B
protected $listen = [
    UserRegistered::class => [
        SendWelcomeEmail::class,
    ],
];
C
protected $listen = [
    SendWelcomeEmail::class => UserRegistered::class,
];
D
protected $listeners = [
    UserRegistered => SendWelcomeEmail,
];
Attempts:
2 left
💡 Hint
Check the property name and the array structure for event to listeners mapping.
🔧 Debug
advanced
2: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], ];
AThe listener class does not have a constructor, so it won't be called.
BThe handle method should be named onUserLoggedIn to work properly.
CThe event class name in the $listen array is incorrect; it should be UserLoggedIn::class, not UserLogin::class.
DThe Log facade is not imported, causing a fatal error.
Attempts:
2 left
💡 Hint
Check the event class names used in the listener and the provider.
state_output
advanced
2: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?
A'completed', because the last listener to run overwrites the status.
B'processing', because the first listener sets the status and it cannot be changed.
C'pending', because event data is immutable and cannot be changed by listeners.
DAn error occurs because two listeners modify the same property.
Attempts:
2 left
💡 Hint
Think about how listeners run and modify shared event data.
🧠 Conceptual
expert
2:00remaining
Which statement about Laravel event listeners is true?
Select the correct statement about Laravel event listeners.
AListeners can be queued to run asynchronously by implementing the ShouldQueue interface.
BListeners must always be registered manually in the EventServiceProvider to work.
CListeners cannot access the event data passed to them.
DListeners automatically retry on failure without any configuration.
Attempts:
2 left
💡 Hint
Think about how Laravel handles long-running or delayed listener tasks.