Event dispatching helps your app respond to actions by sending messages that other parts can listen to and react.
Event dispatching in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Laravel
event(new EventName($data));
Use the
event() helper to dispatch events easily.Events are usually classes that hold data and describe what happened.
Examples
UserRegistered event with the user data.Laravel
event(new UserRegistered($user));
OrderPlaced event to notify listeners.Laravel
event(new OrderPlaced($order));
Sample Program
This example shows how to create a simple event UserRegistered with user data. Then it dispatches the event using event(). Finally, it simulates a listener reacting by printing a welcome message.
Laravel
<?php namespace App\Events; class UserRegistered { public $user; public function __construct($user) { $this->user = $user; } } // In a controller or service $user = (object) ['name' => 'Alice']; event(new UserRegistered($user)); // Listener example (usually in separate class) // Just for demonstration: function onUserRegistered($event) { echo "Welcome, {$event->user->name}!\n"; } // Simulate listener call onUserRegistered(new UserRegistered($user));
Important Notes
Events help keep your code clean by separating what happens from how you react.
Listeners are classes or functions that handle events when they are dispatched.
Laravel can automatically discover and run listeners if you register them properly.
Summary
Event dispatching sends messages about actions in your app.
Use event(new EventName($data)) to dispatch events.
Listeners respond to events to perform tasks like notifications or logging.
Practice
1. What is the main purpose of event dispatching in Laravel?
easy
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 CQuick 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
Solution
Step 1: Recall Laravel event dispatch syntax
Laravel uses the globalevent()helper with a new event instance.Step 2: Match syntax with options
Onlyevent(new UserRegistered($user));matches Laravel's pattern.Final Answer:
event(new UserRegistered($user)); -> Option DQuick 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
Solution
Step 1: Understand event dispatch and listener
The eventOrderPlacedis 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 AQuick 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
Solution
Step 1: Check event dispatch syntax
Laravel requires creating a new instance of the event class usingnew.Step 2: Identify missing 'new' keyword
The code callsUserRegistered($user)like a function, missingnew.Final Answer:
Missing the 'new' keyword before the event class -> Option AQuick 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
Solution
Step 1: Understand Laravel's model change detection
TheisDirty('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 BQuick Check:
Use isDirty() to check changes before event [OK]
Hint: Use isDirty('field') to check changes before event [OK]
Common Mistakes:
- Using incorrect syntax for condition
- Calling dispatch without new keyword
- Checking wrong condition for email change
