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
Laravel Event Dispatching Basics
📖 Scenario: You are building a simple Laravel application that needs to notify when a new user registers. You will use Laravel's event dispatching system to handle this notification.
🎯 Goal: Create a Laravel event and listener to dispatch a UserRegistered event when a new user is created, and handle it by logging a message.
📋 What You'll Learn
Create an event class named UserRegistered with a public property user.
Create a listener class named SendWelcomeEmail that accepts the UserRegistered event.
Dispatch the UserRegistered event after creating a new user instance.
Register the event and listener in the EventServiceProvider.
💡 Why This Matters
🌍 Real World
Event dispatching in Laravel helps separate concerns by letting you trigger actions when something important happens, like user registration, without mixing code.
💼 Career
Understanding Laravel events and listeners is essential for building scalable and maintainable web applications, a common requirement for Laravel developer roles.
Progress0 / 4 steps
1
Create the UserRegistered event class
Create a Laravel event class named UserRegistered in the app/Events directory. It should have a public property called user and a constructor that accepts a $user parameter and assigns it to the property.
Laravel
Hint
Remember to add the __construct method and assign the $user parameter to the public property user.
2
Create the SendWelcomeEmail listener class
Create a Laravel listener class named SendWelcomeEmail in the app/Listeners directory. It should have a handle method that accepts a UserRegistered event instance as a parameter.
Laravel
Hint
Define the handle method with a parameter typed as UserRegistered.
3
Dispatch the UserRegistered event after user creation
In a controller or service, after creating a new user instance stored in the variable $user, dispatch the UserRegistered event by passing $user to it.
Laravel
Hint
Use the event() helper function to dispatch the event with the $user instance.
4
Register the event and listener in EventServiceProvider
In the app/Providers/EventServiceProvider.php file, add the UserRegistered event and SendWelcomeEmail listener to the $listen array property.
Laravel
Hint
Map the event class to the listener class inside the $listen array.
Practice
(1/5)
1. What is the main purpose of event dispatching in Laravel?
easy
A. To create new routes for the application
B. To directly update the database records
C. To send messages about actions happening in the application
D. To handle user authentication automatically
Solution
Step 1: Understand event dispatching concept
Event dispatching is used to send messages or signals when something happens in the app.
Step 2: Identify the correct purpose
It is not about database updates, routing, or authentication but about notifying parts of the app.
Final Answer:
To send messages about actions happening in the application -> Option C
Quick Check:
Event dispatching = sending messages [OK]
Hint: Events notify parts of app about actions [OK]
Common Mistakes:
Confusing events with database operations
Thinking events create routes
Assuming events handle authentication
2. Which of the following is the correct syntax to dispatch an event named UserRegistered with a user object $user?
easy
A. sendEvent('UserRegistered', $user);
B. dispatch(UserRegistered, $user);
C. UserRegistered::dispatch($user);
D. event(new UserRegistered($user));
Solution
Step 1: Recall Laravel event dispatch syntax
Laravel uses the global event() helper with a new event instance.
Step 2: Match syntax with options
Only event(new UserRegistered($user)); matches Laravel's pattern.
Final Answer:
event(new UserRegistered($user)); -> Option D
Quick Check:
Use event(new EventName($data)) [OK]
Hint: Use event(new EventName($data)) to dispatch [OK]
Common Mistakes:
Using dispatch() without new keyword
Calling event as a static method
Using non-existent sendEvent function
3. Given the following code, what will be the output?
class OrderPlaced {
public $orderId;
public function __construct($orderId) {
$this->orderId = $orderId;
}
}
// Dispatch event
event(new OrderPlaced(123));
// Listener logs the order ID
Log::info('Order placed with ID: ' . 123);
medium
A. Logs: 'Order placed with ID: 123'
B. Logs: 'Order placed with ID: OrderPlaced'
C. No log output because event is not handled
D. Syntax error due to missing listener
Solution
Step 1: Understand event dispatch and listener
The event OrderPlaced is dispatched with order ID 123, and the listener logs this ID.
Step 2: Check the log output
The log line explicitly outputs 'Order placed with ID: 123'.
Final Answer:
Logs: 'Order placed with ID: 123' -> Option A
Quick Check:
Listener logs correct order ID [OK]
Hint: Listener logs event data as given [OK]
Common Mistakes:
Assuming event dispatch alone logs output
Confusing event class name with data
Expecting error without listener registration
4. Identify the error in this event dispatching code:
event(UserRegistered($user));
medium
A. Missing the 'new' keyword before the event class
B. Incorrect event name, should be 'UserRegister'
C. Event dispatching requires a string, not an object
D. The variable $user is not passed correctly
Solution
Step 1: Check event dispatch syntax
Laravel requires creating a new instance of the event class using new.
Step 2: Identify missing 'new' keyword
The code calls UserRegistered($user) like a function, missing new.
Final Answer:
Missing the 'new' keyword before the event class -> Option A
Quick Check:
Use new EventClass($data) when dispatching [OK]
Hint: Always use new keyword before event class [OK]
Common Mistakes:
Calling event class as function without new
Wrong event class name
Passing wrong data type
5. You want to dispatch an event ProfileUpdated only if the user's email has changed. Which code correctly implements this conditional event dispatch?
hard
A. ProfileUpdated::dispatch($user) when $user->email != old_email;
B. if ($user->isDirty('email')) { event(new ProfileUpdated($user)); }
C. event(new ProfileUpdated($user)) if $user->email_changed();
D. if ($user->email !== null) { dispatch(ProfileUpdated($user)); }
Solution
Step 1: Understand Laravel's model change detection
The isDirty('email') method checks if the email attribute changed.
Step 2: Verify correct conditional event dispatch
if ($user->isDirty('email')) { event(new ProfileUpdated($user)); } uses correct syntax and condition to dispatch event only if email changed.
Final Answer:
if ($user->isDirty('email')) { event(new ProfileUpdated($user)); } -> Option B
Quick Check:
Use isDirty() to check changes before event [OK]
Hint: Use isDirty('field') to check changes before event [OK]