1

Is it possible to use a queue inside event-listener in Laravel?

Lets say I have a controller QuotesController from where I fire the QuoteCreated event which has a SendNotification listener. And I want to dispatch a queue named EmailToUser from that listener.

Of course I can dispatch the queue directly form Controller but can I dispatch it form a listener?

5
  • 1
    yes you can. dispatch the queue from the handle() method. Commented Oct 26, 2016 at 16:39
  • 1
    I have tied but getting this error Call to undefined method App\Jobs\EmailToUser::dispatch() Commented Oct 26, 2016 at 16:42
  • 1
    share your code Commented Oct 26, 2016 at 16:47
  • 1
    @shuvrow use the global dispatch() helper method. Do not use $this->dispatch(). Commented Oct 26, 2016 at 17:21
  • Thanks it worked, I thinks you should put your comment in answar section now :) @jszobody Commented Oct 26, 2016 at 17:26

1 Answer 1

3

There is a global dispatch() helper method that you can use anywhere in a Laravel app!

dispatch(new SomeJob());

And yes you can do this from inside an event listener, or a queue job, or anything else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.