0

I'm using an observer class in my laravel project and it's throwing a mysterious error. The trace is below. It appears after some searching that in the /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(237) class, the dispatch method attempts to run the listener on a null payload which I believe is the model instance passed to the listener. The model event I'm using is the "saving" event but the controller action being called is only performing a select query and it's not making any changes so I would not expect this event to be thrown.

I have confirmed that by not registering the observer in a service provider, the error ceases to exist, however when I register the observer and comment out any methods in the observer class, it still occurs.

Is this a laravel bug?

I included:

  • controller action
  • Dispatcher class
  • exception trace

** edit **

  • service provider boot method

Controller Action:

    /**
     * Display a listing of the resource.
     *
     * @param App\Models\Crop $crop
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $crop = null;
        if ($request->filled('crop_id')) {
            $crop = Crop::find($request->query('crop_id'));
            $chyps = $crop->harvest_year_pubs->withoutGlobalScopes();
        } else {
            $chyps = CropHarvestYearPublication::orderBy('crop_id');
        }

        if ($request->filled('sort_by')) {
            if ($request->query('sort_order') === 'desc') {
                $chyps->sortByDesc($request->query('sort_by'));
            } else {
                $chyps->sortBy($request->query('sort_by'));
            }
        }

        return view('admin.data.chyp.index', [
            'chyps' => $chyps->paginate(15),
            'crop'  => $crop,
            'crops' => Crop::withoutGlobalScopes()->get(),
            'sort_by' => $request->query('sort_by'),
            'sort_order' => $request->query('sort_order')
        ]);
    }

/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php line 237 is where the error occurs:

   /**
     * Fire an event and call the listeners.
     *
     * @param  string|object  $event
     * @param  mixed  $payload
     * @param  bool  $halt
     * @return array|null
     */
    public function dispatch($event, $payload = [], $halt = false)
    {
        // When the given "event" is actually an object we will assume it is an event
        // object and use the class as the event name and this event itself as the
        // payload to the handler, which makes object based events quite simple.
        [$event, $payload] = $this->parseEventAndPayload(
            $event, $payload
        );

        if ($this->shouldBroadcast($payload)) {
            $this->broadcastEvent($payload[0]);
        }

        $responses = [];

        foreach ($this->getListeners($event) as $listener) {
            $response = $listener($event, $payload); // LINE 237 - where the error is thrown

            // If a response is returned from the listener and event halting is enabled
            // we will just return this response, and not call the rest of the event
            // listeners. Otherwise we will add the response on the response list.
            if ($halt && ! is_null($response)) {
                return $response;
            }

            // If a boolean false is returned from a listener, we will stop propagating
            // the event to any further listeners down in the chain, else we keep on
            // looping through the listeners and firing every one in our sequence.
            if ($response === false) {
                break;
            }

            $responses[] = $response;
        }

        return $halt ? null : $responses;
    }

The Exception Trace:

[2021-02-17 14:33:44] testing.ERROR: Function name must be a string {"userId":1,"exception":"[object] (Error(code: 0): Function name must be a string at /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:392)
[stacktrace]
#0 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(237): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}('eloquent.retrie...', Array)
#1 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(189): Illuminate\\Events\\Dispatcher->dispatch('eloquent.retrie...', Array)
#2 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(434): Illuminate\\Database\\Eloquent\\Model->fireModelEvent('retrieved', false)
#3 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(345): Illuminate\\Database\\Eloquent\\Model->newFromBuilder(Object(stdClass))
#4 [internal function]: Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}(Object(stdClass))
#5 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(344): array_map(Object(Closure), Array)
#6 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php(23): Illuminate\\Database\\Eloquent\\Builder->hydrate(Array)
#7 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1885): Illuminate\\Database\\Eloquent\\Model->forwardCallTo(Object(Illuminate\\Database\\Eloquent\\Builder), 'hydrate', Array)
#8 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(588): Illuminate\\Database\\Eloquent\\Model->__call('hydrate', Array)
#9 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(572): Illuminate\\Database\\Eloquent\\Builder->getModels(Array)
#10 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(772): Illuminate\\Database\\Eloquent\\Builder->get(Array)
#11 /Users/apages/code/variety-testing/app/Http/Controllers/Admin/CropHarvestYearPublicationController.php(38): Illuminate\\Database\\Eloquent\\Builder->paginate(15)
#12 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\Admin\\CropHarvestYearPublicationController->index(Object(Illuminate\\Http\\Request))
#13 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('index', Array)
#14 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Route.php(254): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Admin\\CropHarvestYearPublicationController), 'index')
#15 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): Illuminate\\Routing\\Route->runController()
#16 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\\Routing\\Route->run()
#17 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#18 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#19 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#20 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#21 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#22 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#23 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#24 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#25 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest(Object(Illuminate\\Http\\Request), Object(Illuminate\\Session\\Store), Object(Closure))
#26 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 /Users/apages/code/variety-testing/app/Http/Middleware/CASAuth.php(46): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\CASAuth->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'admin')
#29 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#31 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#32 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#33 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#34 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#35 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Router.php(670): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#36 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#37 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#38 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#39 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#40 /Users/apages/code/variety-testing/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(60): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#41 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#42 /Users/apages/code/variety-testing/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#43 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#44 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#45 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#46 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#47 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#48 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#49 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#50 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#51 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#52 /Users/apages/code/variety-testing/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#53 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#54 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#55 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#56 /Users/apages/code/variety-testing/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#57 /Users/apages/code/variety-testing/public/index.php(17): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#58 /Users/apages/code/variety-testing/server.php(21): require_once('/Users/apages/c...')
#59 {main}

Service provider boot method

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
 
        CropHarvestYearPublication::observe(CropHarvestYearPublication::class);

    }
2
  • Check name spaces , also provide the files up the dependency graph i.e. your registration code in event service provider etc. Also as a side note are you generating using the cli? Commented Feb 18, 2021 at 16:34
  • I verified that the namespaces in the observer, model, service provider and controller classes are correct. I included the line in the service provider in the main section. And yes I generated the observer using the cli, but even when the observer class is empty, I still get the error. Commented Feb 18, 2021 at 17:40

1 Answer 1

0

based on your latest edit I see one problem, you are using same class names for your observer and model I would recommend you to alias either your model or your observer like so

Aliasing model

use App\Models\CropHarvestYearPublication as CropHarvestYearPublicationModel;
use App\Observers\CropHarvestYearPublication;

/**
 * Register any events for your application.
 *
 * @return void
 */
public function boot()
{
    CropHarvestYearPublicationModel::observe(CropHarvestYearPublication::class);
}

Aliasing observer

use App\Models\CropHarvestYearPublication;
use App\Observers\CropHarvestYearPublication as CropHarvestYearPublicationObserver;

/**
 * Register any events for your application.
 *
 * @return void
 */
public function boot()
{
    CropHarvestYearPublication::observe(CropHarvestYearPublicationObserver::class);
}

That's probably the reason for your error. Give it a try

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.