Aivis Silins  •  12 May 2017

A Complete List Of Laravel Events

One of the best features of the Laravel framework is the ability to extend or change it's core components. And to make it even more flexible, Laravel also includes a simple observer implementation where you can subscribe to and listen for events that occur in your application. Today, I'm going to describe a few of the events that are triggered by Laravel, but you can find a complete list at the bottom of this article. Note that Laravel framework event names may differ for each version - I'll be discussing Laravel 5.3 events in this article.

cache:cleared

This event name describes itself - it is fired after the cache has been cleared and only if you use Artisan command cache:clear. Keep in mind that this event is not fired if you clear the cache programmatically with Cache::forget('key') or Cache::flush().

php artisan cache:clear memcached --tags=tagName

Event::listen('cache:cleared', function ($store, $tags) {
    // $store => "memcached"
    // $tags => ["tagName"]
});

illuminate.log

This event enables you to listen for application logs. However, it does not observe exceptions because the Laravel exception handler directly calls Psr\Log\LoggerInterface->error instance (see \App\Exceptions\Handler) which means that only messages logged by Laravel \Log facade (or via the IOC container) will be available.

If you're interested in listening for all of your application logs then it will be necessary to create a Monolog handler and bind it to Psr\Log\LoggerInterface. See the Monolog documentation if you would like to know more about this.

Log::info('My awesome log message', ['key' => 'value']);

Event::listen('illuminate.log', function ($level, $message, $context) {
    // $level => "info"
    // $message => "My awesome log message"
    // $context => ["key" => "value"]
});

kernel.handled

This event is fired right after an object is received from the response handler and before it's returned (to the browser or console).

Event::listen('kernel.handled', function ($request, $response) {
    // $request => Illuminate\Http\Request
    // $response => Illuminate\Http\Response
});

\Illuminate\Queue\Events\JobProcessed::class

This event could be useful if you would like to store all processed queue jobs in a MySQL table. All necessary data is available within the event object, including the job payload.

Event::listen(\Illuminate\Queue\Events\JobProcessed::class, function ($event) {
    // $event->job => Illuminate\Queue\Jobs\SyncJob
    // $event->connectionName => "sync"
});

Final thoughts

Events allow you to observe your framework state at specific points, and can be put to use in a variety of scenarios. Hopefully, this article will help give you some insight into what's available under the hood.

Links to Resources

Complete list of Laravel 5.3 core events: