Laravel Artisan Event Command: The event:generate Command

December 7, 2016 —John Koster

The event:generate command is used to generate missing events and listeners based on the events that are registered in the applications EventServiceProvider (by default this service provider is located in the app/Providers/EventServiceProvider.php file).

Assuming the following EventServiceProvider:

3namespace App\Providers;
5use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
6use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
8class EventServiceProvider extends ServiceProvider
10 /**
11 * The event listener mappings for the application.
12 *
13 * @var array
14 */
15 protected $listen = [
16 'App\Events\BookWasPurchased' => [
17 'App\Listeners\EmailPurchaseConfirmation',
18 ],
19 ];
21 /**
22 * Register any other events for your application.
23 *
24 * @param \Illuminate\Contracts\Events\Dispatcher $events
25 * @return void
26 */
27 public function boot(DispatcherContract $events)
28 {
29 parent::boot($events);
31 //
32 }

We can generate an Event class for BookWasPurchased and an EventListener class for EmailPurchaseConfirmation like so:

1# Generate missing events and listeners.
2php artisan event:generate

The newly created event and listener will be stored in the following files, respectively: app/Events/BookWasPurchased.php and app/Listeners/EmailPurchaseConfirmation.php.

It should also be noted that this command will not overwrite any previously generated events or listeners and can be ran as many times as necessary.

#Manually Generating Events and Listeners

The event:generate command is simply a convenient wrapper around the make:listener and make:event commands.

The classes generated in the previous example can be manually generated like so:

1# Generate the BookWasPurchasedEvent event class.
2php artisan make:event BookWasPurchasedEvent
4# Generate the EmailPurchaseConfirmation listener class.
5php artisan EmailPurchaseConfirmation --event=BookWasPurchasedEvent

Some absolutely amazing

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.