Introduction
As applications grow, you want to keep your code modular and decoupled. Instead of writing all logic inside controllers or models, Laravel provides an Events & Listeners system.
Events represent something that happened (e.g., UserRegistered).
Listeners respond to those events (e.g., SendWelcomeEmail).
This makes your application more maintainable and scalable. For interviews, questions like “How do events work in Laravel?” or “What’s the difference between observers and listeners?” are very common.
What are Events in Laravel?
Events capture something significant happening in your application:
- User registered
- Order placed
- Payment completed
Instead of executing actions directly, you fire an event and attach listeners to handle it.
What are Listeners in Laravel?
Listeners are actions triggered when an event occurs.
Example:
- Event:
UserRegistered - Listeners:
SendWelcomeEmail,LogRegistrationActivity,NotifyAdmin
👉 This approach makes your code more modular and testable.
Creating Events & Listeners
Step 1: Generate Event & Listener
php artisan make:event UserRegistered php artisan make:listener SendWelcomeEmail --event=UserRegistered
Step 2: Define Event
app/Events/UserRegistered.php
class UserRegistered {
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public function __construct(User $user) {
$this->user = $user;
}
}
Step 3: Define Listener
app/Listeners/SendWelcomeEmail.php
class SendWelcomeEmail {
public function handle(UserRegistered $event) {
Mail::to($event->user->email)->send(new WelcomeMail($event->user));
}
}
Step 4: Fire Event in Controller
public function register(Request $request) {
$user = User::create($request->all());
event(new UserRegistered($user));
return response()->json(['message' => 'User registered successfully'], 201);
}
👉 When a user registers, the event triggers, and the listener sends the welcome email.
Event Service Provider
Laravel automatically registers events and listeners in app/Providers/EventServiceProvider.php:
protected $listen = [
UserRegistered::class => [
SendWelcomeEmail::class,
],
];
Queued Listeners
Listeners can run asynchronously using queues for better performance:
class SendWelcomeEmail implements ShouldQueue {
public function handle(UserRegistered $event) {
// Email logic
}
}
👉 Now the listener will run in the background via Laravel’s queue system.
Events vs Observers vs Listeners
| Concept | Purpose | Example Use Case |
|---|---|---|
| Event | Represents something that happened | UserRegistered |
| Listener | Responds to an event | SendWelcomeEmail |
| Observer | Tied to Eloquent models (create, update, delete) | UserObserver watching User model |
👉 Observers are specific to models, while Events & Listeners are more general.
Real-World Use Cases
- E-commerce:
OrderPlacedevent → send invoice, notify admin, update stock. - Social App:
PostCreatedevent → notify followers, log activity. - API:
PaymentCompletedevent → update DB, trigger webhook, send email.
Common Beginner Mistakes
- Writing all logic inside controllers instead of decoupling with events.
- Forgetting to register events in EventServiceProvider.
- Not queuing heavy listeners (causes slow API responses).
- Confusing events with observers.
Sample Interview Questions & Answers
Q: What is the difference between events and listeners in Laravel?
A: Events represent something that happened, listeners handle the action triggered by that event.
Q: How do you fire an event in Laravel?
A: Using event(new EventName($data));.
Q: How do you run listeners asynchronously?
A: By implementing the ShouldQueue interface in the listener.
Q: What’s the difference between an Event Listener and an Observer?
A: Observers are tied to model lifecycle events, while listeners can respond to any custom event.
Q: Can a single event have multiple listeners?
A: Yes, and each listener can perform different actions.
Q: Why use events instead of putting logic in controllers?
A: Events decouple business logic, make apps more modular, and allow multiple actions per event.
Mini Project Idea
👉 Build an Order Processing System:
- Event:
OrderPlaced. - Listeners:
SendOrderConfirmationEmailNotifyWarehouseUpdateAnalytics
- 👉 Add
ShouldQueueto listeners for async processing.
Closing Note
Laravel’s Events & Listeners provide a clean way to decouple logic and make applications more modular. Combined with queues, they enable apps to handle complex workflows efficiently.
Laravel Framework Mastery
Laravel as an API Backend
→ Build powerful, scalable APIs with Laravel, leveraging routes, controllers, and resources for seamless data handling and integration
Laravel Passport vs Sanctum
→ Understand the key differences between Laravel Passport and Sanctum to choose the right authentication method for your web and mobile APIs
Laravel Queues & Jobs for APIs
→ Optimize performance and scalability by offloading time-consuming API tasks using Laravel’s powerful queue and job processing system























0 Comments