Himanshu SharmaSoftware engineer
Let’s talk

Laravel · Lesson 32 of 40

Events and listeners in Laravel

Announce what happened with an event class and react in separate listeners, rely on automatic discovery, check wiring with event:list and queue listeners.

13 minBackground work and cachingUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCreate the ReviewPosted event and a listener, see how Laravel finds listeners automatically, check them with event:list, and explain when events beat direct calls.

What you can do after this lesson

You will split "a review was posted" from "what should happen because of it". Right now each review controller dispatches a job. After this lesson they announce an event, and more reactions, such as email and cache clearing in later lessons, can attach without touching the controllers again.

The problem events solve

When a review is saved, several things could follow: log it, email an administrator, refresh a cached rating. If the controller calls each of those directly, it grows and learns about every consumer, and the web controller and the API controller must be kept in sync. An event reverses that. The controller says "a review was posted" once. Any number of listeners subscribe to that fact.

Creating an event and a listener

bash
php artisan make:event ReviewPosted
php artisan make:listener QueueActivityRecord --event=ReviewPosted
text
   INFO  Event [app/Events/ReviewPosted.php] created successfully.
   INFO  Listener [app/Listeners/QueueActivityRecord.php] created successfully.

The generated event includes broadcasting scaffolding (for sending events to browsers over websockets) that this project does not use, so the finished class trims it back to what we need: an event is a plain class that carries data.

php
<?php

namespace App\Events;

use App\Models\Review;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ReviewPosted
{
    use Dispatchable, SerializesModels;

    public function __construct(public Review $review)
    {
    }
}

Dispatchable adds the static ReviewPosted::dispatch(...). SerializesModels matters when a queued listener receives the event: the review is stored as an id and reloaded. The listener reacts, and here simply hands the work to the job from the previous lesson:

php
<?php

namespace App\Listeners;

use App\Events\ReviewPosted;
use App\Jobs\RecordReviewActivity;

class QueueActivityRecord
{
    public function handle(ReviewPosted $event): void
    {
        RecordReviewActivity::dispatch($event->review);
    }
}

In both review controllers the line RecordReviewActivity::dispatch($review); became:

php
ReviewPosted::dispatch($review);

No registration needed

In older Laravel versions you listed events and listeners in an EventServiceProvider. Laravel 13 discovers listeners in app/Listeners automatically: for each public handle (or __invoke) method it reads the type of the first argument, and if that is an event class, the listener is wired to it. Check what is wired:

bash
php artisan event:list --event=ReviewPosted
text
  App\Events\ReviewPosted ....................................................
  ⇂ App\Listeners\QueueActivityRecord@handle

Without the --event filter the full list includes framework and package listeners too, such as the SendEmailVerificationNotification listener for the built-in Registered event. For production, php artisan event:cache stores the discovered map so discovery does not run on every request (and event:clear removes it); the deployment lesson mentions it. You can also register a listener manually with Event::listen(...) in a service provider.

Test the flow end to end. A logged-in user posted a review through the web form; the result was one job on the queue (made by the listener), and after a worker ran, one Review activity line in the log:

text
1 job(s)
  2026-11-15 08:02:33 App\Jobs\RecordReviewActivity .............. 9.71ms DONE

The behaviour is identical to the previous lesson, but the controllers no longer know about the job.

Queued listeners

A listener that implements Illuminate\Contracts\Queue\ShouldQueue runs on the queue instead of during the request. Add it to a class to make the reaction itself asynchronous:

php
use Illuminate\Contracts\Queue\ShouldQueue;

class NotifyAdminsOfReview implements ShouldQueue
{
    public function handle(ReviewPosted $event): void
    {
        // slow work here
    }
}

You will write exactly this class in the next lesson, to email the shop's administrators. Whether to make a listener queued depends on how slow and how critical it is. Queued listeners need a running worker.

Events versus observers and plain calls

Eloquent also fires model events (created, updated, deleted), which you can handle with observers (php artisan make:observer); they suit reactions that must happen whenever a model changes, whatever code changed it. Custom events like ReviewPosted describe business facts and are usually clearer. For a single obvious step, a direct method call is simpler than an event: do not use events for everything. A good test: would you plausibly add or remove reactions without changing the code that triggers them?

Common mistakes

  • Putting logic in the event class. Events carry data; listeners act.
  • Forgetting that a non-queued listener runs inside the request and can slow or break it.
  • Listeners with the wrong type hint, so discovery skips them. Check event:list.
  • Event chains that are hard to follow. Keep it shallow and documented.
  • Forgetting event:clear after changing listeners when an event cache exists.

Exercise

Create a second listener LogReviewRating for ReviewPosted that writes Log::info('Rating posted', ['rating' => ...]) right away, and verify with event:list that both listeners appear.

Show one possible answer
bash
php artisan make:listener LogReviewRating --event=ReviewPosted
php
public function handle(ReviewPosted $event): void
{
    Log::info('Rating posted', ['rating' => $event->review->rating]);
}

php artisan event:list --event=ReviewPosted now shows both QueueActivityRecord@handle and LogReviewRating@handle under the event. This listener runs during the request because it does not implement ShouldQueue. Next: sending mail.

Key points

  • An event is a small class describing something that happened; listeners are separate classes that react to it.
  • Events decouple the code that causes a thing (saving a review) from the code that responds (logging, mail, cache), so you can add reactions without editing the controller.
  • In Laravel 13 listeners in app/Listeners are discovered automatically from the type of their handle method's argument.
  • php artisan event:list --event=ReviewPosted shows which listeners are wired to an event.
  • A listener that implements ShouldQueue runs on the queue; otherwise it runs immediately during the dispatch.

Check yourself

1. What is the main benefit of dispatching an event instead of calling each follow-up directly?
2. How does Laravel 13 know a listener should handle ReviewPosted?
3. Which command shows the listeners of one event?
4. A listener implements ShouldQueue. When does it run?