Commands Vs Events

When the Laravel 5 version was released, the last implementation of command bus also got implemented.

If you want to do a command, you have to call it somewhere else in the code. When an event occurs in your application an event responds to it. Let’s see the difference using an example.

AdminController {

 

    function create() {

            Bus::dispatch(new CreateUser(Auth::user());

    }

}

In the CommandClass you should add the code given below.

public function handle(CreateUser $auth)

{

     // 1. Create the user here

     // 2. Send welcome email

     // 3. Update our newsletter

}

If you are using an event then you should do something like this in the CommandClass:

public function handle(CreateUser $auth)

    {

         // 1. Create the user here

         Event::fire(new UserWasCreated($user));

    }

Then you can create any number of event.

pre>Event::listen('UserWasCreated', function($event)

{

    // 2. Send welcome email

});