Home > front end >  Laravel Artisan event:generate command throwing a Fatal error
Laravel Artisan event:generate command throwing a Fatal error

Time:10-26

I'm following the official documentation for Laravel 5.7 on the events registration and generation: https://laravel.com/docs/5.7/events#generating-events-and-listeners

I have an EventServiceProvider with the following events defined:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\FormBeforeCreate' => [
            'App\Listeners\WebhookBeforeCreate',
        ],
        'App\Events\FormAfterCreate' => [
            'App\Listeners\NotifyAfterCreate',
            'App\Listeners\WebhookAfterCreate',
        ],
        'App\Events\FormBeforeUpdate' => [
            'App\Listeners\WebhookBeforeUpdate',
        ],
        'App\Events\FormAfterUpdate' => [
            'App\Listeners\NotifyAfterUpdate',
            'App\Listeners\WebhookAfterUpdate',
        ],
        'App\Events\FormBeforeDelete' => [
            'App\Listeners\WebhookBeforeDelete',
        ],
        'App\Events\FormAfterDelete' => [
            'App\Listeners\NotifyAfterDelete',
            'App\Listeners\WebhookAfterDelete',
        ],
        'App\Events\FormBeforeSave' => [
            'App\Listeners\WebhookBeforeSave',
        ],
        'App\Events\FormAfterSave' => [
            'App\Listeners\NotifyAfterSave',
            'App\Listeners\WebhookAfterSave',
        ],
    ];

    /**
     * The subscriber classes to register.
     *
     * @var array
     */
    protected $subscribe = [
        'App\Listeners\UserEventSubscriber',
    ];

    /**
     * Register any other events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    }
}

The error:

When I run the command php artisan event:generate I get the following error:

PHP Fatal error:  Call to a member function listens() on null in /app/vendor/laravel/framework/src/Illuminate/Foundation/Console/EventGenerateCommand.php on line 35

                                                           
  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Call to a member function listens() on null

According to the doc, it should do this:

This command will generate any events or listeners that are listed in your EventServiceProvider. Events and listeners that already exist will be left untouched

I don't understant what I've missed since I didn't find any similar error by searching the web

CodePudding user response:

This is the line that is returning null:

$providers = $this->laravel->getProviders(EventServiceProvider::class);

therefore, there are some problems with your EventServiceProvider... please, try using this:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\FormBeforeCreate' => [
            'App\Listeners\WebhookBeforeCreate',
        ],
        'App\Events\FormAfterCreate' => [
            'App\Listeners\NotifyAfterCreate',
            'App\Listeners\WebhookAfterCreate',
        ],
        'App\Events\FormBeforeUpdate' => [
            'App\Listeners\WebhookBeforeUpdate',
        ],
        'App\Events\FormAfterUpdate' => [
            'App\Listeners\NotifyAfterUpdate',
            'App\Listeners\WebhookAfterUpdate',
        ],
        'App\Events\FormBeforeDelete' => [
            'App\Listeners\WebhookBeforeDelete',
        ],
        'App\Events\FormAfterDelete' => [
            'App\Listeners\NotifyAfterDelete',
            'App\Listeners\WebhookAfterDelete',
        ],
        'App\Events\FormBeforeSave' => [
            'App\Listeners\WebhookBeforeSave',
        ],
        'App\Events\FormAfterSave' => [
            'App\Listeners\NotifyAfterSave',
            'App\Listeners\WebhookAfterSave',
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

CodePudding user response:

My bad, it seems like I was really tired yesterday night, Our project runs on Docker, I was running the command outside of the docker instead of inside.

I have no idea why it showed this bug in particular but once I ran the command in the docker all files generated correctly.

  • Related