Home API Tools Posts Hire Me About
Sign In Create Account
Laravel

Laravel Events and Listeners: Decouple Your Code

January 16, 2026 1 min read 57 views

Decouple code with events:

Create Event

class OrderPlaced
{
    public function __construct(public Order $order) {}
}

Create Listener

class SendOrderConfirmation
{
    public function handle(OrderPlaced $event)
    {
        Mail::to($event->order->user)->send(new OrderConfirmationMail($event->order));
    }
}

Dispatch Event

OrderPlaced::dispatch($order);
event(new OrderPlaced($order));

Register in EventServiceProvider

protected $listen = [
    OrderPlaced::class => [
        SendOrderConfirmation::class,
        UpdateInventory::class,
    ],
];
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!