Larascord Events
⚠️

Events were deprecated after version 4.1.3 (opens in a new tab) in favour of Model Observers (opens in a new tab).

Events are created in App\Events during installation.

Listening

Create an event listener

To listen to an event you will have to create a listener. You can do this by running the following command:

php artisan make:listener ListenerName --event=EventName

Example:

php artisan make:listener UserCreated --event=UserWasCreated

Register the listener

You will have to register the listener mappings in App\Providers\EventListenerProvider in the $listen property.

EventListenerProvider.php
/**
* The event to listener mappings for the application.
*
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ],
 
    UserWasCreated::class => [
        UserCreated::class
    ]
];

Now the handle method of UserCreated listener will be executed every time a new user is created.

UserCreated.php
<?php
 
namespace App\Listeners;
 
use App\Events\UserWasCreated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
 
class UserCreated
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
 
    /**
     * Handle the event.
     *
     * @param  \App\Events\UserWasCreated  $event
     * @return void
     */
    public function handle(UserWasCreated $event)
    {
        Log::info('User was created: ' . $event->user->username);
    }
}