Return data from laravel events to controllers

Laravel events are lovely to work with, it makes your controllers much cleaner and it groups multiple tasks. For example, when creating a new user. I used it a lot but I never had to return data back to my controller.

At first, I was a bit worried, I had never seen an example where there was actually data returned to the controller.

But Laravel shouldn’t be Laravel if it was not possible, after a bit of research it seemed that you could just return whatever you want in every listener (in the handle method) of your event. In your controller, you would get an array with a key for each listener you have for the fired event.

# response of the fired event. This event had one listner 

0 => "{"slug":"my-slug","id":1634,"name":"my name","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"XXX","id":123,"name":"Jorenvh","description":"Joren Van Hocht","public":false,"type":"NORMAL","links":{"self":[{"href":"http://github.com/jorenvh"}]}},"public":false,"links":{"clone":[{"href":"http://github.com/jorenvh/blogify.git","name":"http"},{"href":"ssh://git@github.com/jorenvh/blogify.git","name":"ssh"}],"self":[{"href":"http://github.com/jorenvh/blogify"}]}}"]

Each of these keys contain the JSON encoded data that you returned in the handle method of your listener.