How to use Events and Listeners in Laravel?

Posted 5 years ago by Ryan Dhungel

Category: Laravel

Viewed 7922 times

Estimated read time: 1 minute

An example of using Events and Listeners in laravel.

For example while creating a blog, you might want to email the users, subscribe them or add the blog to search index etc.

Instead of doing everything inside single controller method, we can create seperate Events and Listeners for each of those actions:

Create a new blog and use artisan command to create a new event called BlogWasCreated.

art make:event BlogWasCreated


use App\Events\BlogWasCreated;

public function create()
        $blog = new Blog;
        $blog->title = 'New Blog';
        // fire an event
        event(new BlogWasCreated($blog));
        // email
        // subscribe
        // search index


use App\Blog;

class BlogWasCreated

public $blog;

    public function __construct(Blog $blog)
        $this->blog = $blog;


art make:listener EmailUserAboutCreatedBlog --event=BlogWasCreated


public function handle(BlogWasCreated $event)
var_dump('email user');

Define Event and Listeners relationships


protected $listen = [
'App\Events\BlogWasCreated' => [
// go through the same process to add more events here

These are the key steps of using Events and Listeners in Laravel.