Search code examples
laraveleventslistenerlaravel-5.4broadcasting

Why does the events I created with Laravel have the 'App\Events' appended in the beginning?


I created events using Laravel and am utilizing it in conjunction with Pusher. On my Pusher Debug Console, every time I fire off a event from my Laravel application, it shows up as 'App\Events\testEvent', whereas I need it to show up as just 'testEvent' like it does when I fire off a dummy event using the tool in the Pusher Debug Console.

https://www.screencast.com/t/cxQ30SZh5x

How do I type the code into my Laravel application so that it fires off the event as 'testEvent' instead of 'App\Events\testEvent'?

To fire the event, I am just using web.php in routes folder as follows:

Route::get('/testEvent', function() {
  event(new testEvent());
});

Solution

  • You may customize the event name that gets broadcasted using the broadcastAs function on your Event class.

    See: https://laravel.com/docs/5.4/broadcasting#broadcast-name

    public function broadcastAs()
    {
        return 'testEvent';
    }