0
0
Laravelframework~10 mins

Defining events in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining events
Create Event Class
Define Event Properties
Create Listener Class
Register Event and Listener
Fire Event in Code
Listener Handles Event
This flow shows how you create an event class, define its data, create a listener, register both, then fire the event so the listener reacts.
Execution Sample
Laravel
php artisan make:event OrderShipped

class OrderShipped {
  public $order;
  public function __construct($order) {
    $this->order = $order;
  }
}
This code creates an event class named OrderShipped that holds an order object passed when the event is fired.
Execution Table
StepActionCode/CommandResult/State Change
1Create event classphp artisan make:event OrderShippedOrderShipped.php file created with class skeleton
2Add property and constructorpublic $order; public function __construct($order) { $this->order = $order; }Event class now holds order data
3Create listener classphp artisan make:listener SendShipmentNotificationListener class created to handle event
4Register event and listenerIn EventServiceProvider.php, add to $listen arrayLaravel knows which listener to call for OrderShipped
5Fire event in codeevent(new OrderShipped($order));Event dispatched, listeners triggered
6Listener handles eventpublic function handle(OrderShipped $event) { // send notification }Notification sent using event data
7ExitNo more listenersEvent handling complete
💡 All listeners for the event have run, so event processing stops.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
$ordernullpassed to constructorpassed to event instanceused in listener to send notification
OrderShipped instancenonecreated with $orderdispatched via event()passed to listener handle()
Key Moments - 3 Insights
Why do we need to define properties and a constructor in the event class?
Because the event needs to carry data (like $order) to listeners. Step 2 shows adding these so listeners can access event info.
How does Laravel know which listener to run for an event?
In Step 4, the event and listener are registered in EventServiceProvider.php. This mapping tells Laravel which listener handles which event.
What happens when we call event(new OrderShipped($order))?
Step 5 shows firing the event. Laravel dispatches it and calls all registered listeners, as seen in Step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is created at Step 1?
AAn event class file named OrderShipped.php
BA listener class file named SendShipmentNotification.php
CThe EventServiceProvider.php file
DThe notification email
💡 Hint
Check Step 1 in the execution_table where the event class is created.
At which step is the event fired so listeners start working?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look at Step 5 in execution_table where event(new OrderShipped($order)) is called.
If we forget to register the listener in EventServiceProvider, what happens?
AThe listener still runs automatically
BThe event class won't be created
CThe listener won't run when the event fires
DThe event won't fire at all
💡 Hint
Refer to Step 4 where registration links event and listener.
Concept Snapshot
Defining events in Laravel:
1. Create event class with php artisan make:event.
2. Add properties and constructor to pass data.
3. Create listener class.
4. Register event-listener in EventServiceProvider.
5. Fire event with event(new EventName(data));
6. Listener handles event data.
Full Transcript
In Laravel, defining events starts by creating an event class using artisan. This class holds any data you want to pass to listeners, like an order object. Next, you create a listener class that will respond when the event fires. You must register the event and listener in the EventServiceProvider so Laravel knows which listener to call. When you fire the event in your code using event(new EventName(data)), Laravel dispatches it and calls all registered listeners. The listener then handles the event, for example sending a notification. This process helps separate concerns and makes your app more organized.