{"id":"b75ceef18527e0b8","repo":"laravel/framework","slug":"unable-to-find-observer-class","errorCode":null,"errorMessage":"Unable to find observer: {$class}","messagePattern":"Unable to find observer: (.+?)","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasEvents.php","lineNumber":123,"sourceCode":"    /**\n     * Resolve the observer's class name from an object or string.\n     *\n     * @param  object|string  $class\n     * @return class-string\n     *\n     * @throws \\InvalidArgumentException\n     */\n    private function resolveObserverClassName($class)\n    {\n        if (is_object($class)) {\n            return get_class($class);\n        }\n\n        if (class_exists($class)) {\n            return $class;\n        }\n\n        throw new InvalidArgumentException('Unable to find observer: '.$class);\n    }\n\n    /**\n     * Get the observable event names.\n     *\n     * @return string[]\n     */\n    public function getObservableEvents()\n    {\n        return array_merge(\n            [\n                'retrieved', 'creating', 'created', 'updating', 'updated',\n                'saving', 'saved', 'restoring', 'restored', 'replicating',\n                'trashed', 'deleting', 'deleted', 'forceDeleting', 'forceDeleted',\n            ],\n            $this->observables\n        );\n    }","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php#L105-L141","documentation":"When registering an observer via Model::observe($class), resolveObserverClassName() requires the supplied string (or object's class) to actually exist. A non-existent class string is rejected up-front because binding event listeners against it would silently no-op.","triggerScenarios":"Calling Post::observe('App\\Observers\\AuditObserver') when that class is not autoloadable: wrong namespace, typo, file not yet created, or composer autoload cache stale.","commonSituations":"Observer created in a non PSR-4 path; renamed namespace but boot reference not updated; package observers referenced without installing the package; stale composer dump-autoload after a move.","solutions":["Verify the FQN matches the file location (PSR-4) and the class exists.","Run composer dump-autoload to refresh the classmap.","Pass the ::class constant (Post::observe(AuditObserver::class)) instead of a literal string to catch typos at IDE/static-analysis time."],"exampleFix":"// before\nPost::observe('App\\Observer\\PostObserver'); // wrong namespace 'Observer' vs 'Observers'\n\n// after\nPost::observe(\\App\\Observers\\PostObserver::class);","handlingStrategy":"validation","validationCode":"if (! class_exists($observerClass)) {\n    throw new \\InvalidArgumentException(\"Observer class not found: {$observerClass}\");\n}\nPost::observe($observerClass);","typeGuard":"function observerClassExists(string $class): bool\n{\n    return class_exists($class);\n}","tryCatchPattern":"try {\n    Post::observe(\\App\\Observers\\PostObserver::class);\n} catch (\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'Unable to find observer')) {\n        \\Illuminate\\Support\\Facades\\Artisan::call('optimize:clear');\n        report('Observer class missing; cleared autoload cache');\n    }\n    throw $e;\n}","preventionTips":["Pass Observer::class constants instead of string literals.","Place observers in the PSR-4 path declared in composer.json.","Run composer dump-autoload after creating or moving observer files."],"tags":["eloquent","observer","events","class-not-found","config"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}