Saturday, August 13, 2016

Laravel 5.2 queuing Event listeners

First , we need to create Event and to listen event, we also have to create listener. To do this , laravel provides us several ways. The easiest way to create event and corresponding listener we can use this command
php artisan event:generate
To make it work, we have to define our event and corresponding listener in EventServiceProvider.php. So for test purpose lets provide following values in $listen variable.
'App\Events\DeleteBranchCourse' => [
            'App\Listeners\DeleteCourseConfirmation',
        ], 
Now we are ready to run our command
php artisan event:generate
So if we look carefully to Events and Listeners folders, we can see, DeleteBranchCourse and DeleteCourseConfirmation class created respectively. So to make listeners queue , we have to implement ShouldQueue and to manually configure queued jobs, we have to use InteractsWithQueue traits.
class DeleteCourseConfirmation implements ShouldQueue(){
  use InteractsWithQueue; 
}
So to use queue , we have to use database as queue driver. Now we will create a function hello in DeleteBranchCourse Event class and call this function from DeleteCourseConfirmation's handler function
public function handle(DeleteBranchCourse $event)
    {

        $this->attempts($event->hello());
    }
Now we have to fire our event from a controller's function
Event::fire(new DeleteBranchCourse($feeId));

0 comments:

Post a Comment