{"record":{"id":"cc51eaa55f64d510","repo":"laravel/framework","slug":"failed-to-find-resource-class-for-model-s-cc51ea","errorCode":null,"errorMessage":"Failed to find resource class for model [%s].","messagePattern":"Failed to find resource class for model \\[(.+?)\\]\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/TransformsToResource.php","lineNumber":49,"sourceCode":"     * @return \\Illuminate\\Http\\Resources\\Json\\JsonResource\n     *\n     * @throws \\LogicException\n     */\n    protected function guessResource(): JsonResource\n    {\n        $resourceClass = $this->resolveResourceFromAttribute(static::class);\n\n        if ($resourceClass !== null && class_exists($resourceClass)) {\n            return $resourceClass::make($this);\n        }\n\n        foreach (static::guessResourceName() as $resourceClass) {\n            if (is_string($resourceClass) && class_exists($resourceClass)) {\n                return $resourceClass::make($this);\n            }\n        }\n\n        throw new LogicException(sprintf('Failed to find resource class for model [%s].', get_class($this)));\n    }\n\n    /**\n     * Guess the resource class name for the model.\n     *\n     * @return array{class-string<\\Illuminate\\Http\\Resources\\Json\\JsonResource>, class-string<\\Illuminate\\Http\\Resources\\Json\\JsonResource>}\n     */\n    public static function guessResourceName(): array\n    {\n        $modelClass = static::class;\n\n        if (! Str::contains($modelClass, '\\\\Models\\\\')) {\n            return [];\n        }\n\n        $relativeNamespace = Str::after($modelClass, '\\\\Models\\\\');\n\n        $relativeNamespace = Str::contains($relativeNamespace, '\\\\')","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/TransformsToResource.php#L31-L67","documentation":"Thrown by TransformsToResource::guessResource() when toResource() cannot find a JSON resource class for the model. It first checks the UseResource PHP attribute, then guesses two namespace-derived names (e.g. App\\Http\\Resources\\UserResource and App\\Http\\Resources\\User). If neither class exists and there is no attribute, it fails. The guessing only works for models under a \\Models\\ namespace segment.","triggerScenarios":"Calling $model->toResource() on a model that (a) has no #[UseResource(...)] attribute, (b) does not sit in a namespace containing \\Models\\, or (c) sits in \\Models\\ but the corresponding App\\Http\\Resources\\<Name>Resource class was never created.","commonSituations":"Renaming or moving a model without creating the matching resource; using a model outside the conventional Models namespace (e.g. a package model); forgetting to make Artisan resource; calling toResource() in tests without the resource class loaded.","solutions":["Pass the resource class explicitly: $model->toResource(UserResource::class).","Add the #[UseResource(UserResource::class)] attribute to the model class.","Create the resource class in the conventional App\\Http\\Resources namespace (php artisan make:resource UserResource).","Ensure the model lives under a namespace containing \\Models\\ so the guesser can derive the path."],"exampleFix":"// before (model has no resource and toResource() is called)\nreturn $user->toResource();\n\n// after - option 1: explicit class\nreturn $user->toResource(UserResource::class);\n\n// after - option 2: attribute on the model\nuse Illuminate\\Database\\Eloquent\\Attributes\\UseResource;\n#[UseResource(UserResource::class)]\nclass User extends Model { /* ... */ }","handlingStrategy":"validation","validationCode":"// Resolve the resource class up front; fall back to an explicit one\n$resourceClass = $model::guessResourceName()[0] ?? null;\nif (! is_string($resourceClass) || ! class_exists($resourceClass)) {\n    $resourceClass = \\App\\Http\\Resources\\FallbackResource::class;\n}\nreturn $model->toResource($resourceClass);","typeGuard":"function hasResolvableResource(string $modelClass): bool {\n    if (! class_exists($modelClass)) return false;\n    $ref = new \\ReflectionClass($modelClass);\n    if ($ref->getAttributes(\\Illuminate\\Database\\Eloquent\\Attributes\\UseResource::class)) return true;\n    return collect($modelClass::guessResourceName())\n        ->contains(fn ($c) => is_string($c) && class_exists($c));\n}","tryCatchPattern":"try {\n    return $model->toResource();\n} catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'Failed to find resource class')) {\n        return \\App\\Http\\Resources\\FallbackResource::make($model);\n    }\n    throw $e;\n}","preventionTips":["Add the #[UseResource(...)] attribute so resolution does not depend on namespace conventions.","Create the matching *Resource class in App\\Http\\Resources for every model you expose.","Keep models under the conventional \\Models\\ namespace so guessResourceName works.","In tests, either load the resource class or pass it explicitly to toResource()."],"tags":["eloquent","api-resources","configuration","conventions","to-resource"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}