Events and Listeners - Laravel

Events and Listeners – Laravel

In this tutorial, we are going to teach you how you can implement events and listeners in Laravel. Events are created and multiple listeners can be assigned to them. Listeners are responsible for performing an action like saving the data in the database, or sending an email, etc. In your controllers, you only need to emit an event and all the listeners associated with it will be called automatically.

Video Tutorial:

Creating an Event

First, we are going to create an event. Open terminal or command prompt at the root of your Laravel project and run the following command:

php artisan make:event MyEvent

A new file will be created at app/Events/MyEvent.php. Now place all the variables in it that you will be passed while calling this event.

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MyEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $my_value;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($my_value)
    {
        $this->my_value = $my_value;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

Creating a Listener

Now we need to create a listener that will be called when the above event is emitted. Open the terminal and run the following command in it:

php artisan make:listener MyListener

A new file will be created at app/Listeners/MyListener.php. It will have the following content:

<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class MyListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        dd($event->my_value);
    }
}

That $event variable will hold all the values sent from the event class (MyEvent, in this case).

Attach Listeners to Event

The event and listeners are created. Now is the time to tell the Laravel framework to which listeners should be called when a specific event is called. Goto app/Providers/EventServiceProvider.php and include the event and listener at the top:

use App\Events\MyEvent;
use App\Listeners\MyListener;

Then update the $listen array in it:

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    MyEvent::class => [
        MyListener::class
    ]
];

Now, whenever a MyEvent event is emitted, the MyListener listener will automatically be called.

Emitting an Event

Emitting an event in your Laravel project is very easy. You just need to include your event at the top:

use App\Events\MyEvent;

Then whenever you need to call it, you can simply call it like this:

event(new MyEvent("adnan-tech.com"));

At this point, the event will be emitted and the listener will be called. But we need to find a way to check if the listener is actually being called. We can simply do dd() inside listener’s handle() function.

Leave a Reply

Please disable your adblocker or whitelist this site!