{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/TransformsToResource.php#L31-L67","documentation":"Thrown by TransformsToResource::guessResource() when Model::toResource() is called without an explicit class and the framework cannot find a matching resource. The guesser first checks a UseResource attribute on the model, then tries convention-based names derived from the model's namespace (replacing \\Models\\ with \\Http\\Resources\\ and appending 'Resource'). If none of those classes exist, it throws this LogicException.","triggerScenarios":"Calling $model->toResource() (or a route/response that calls it) on a model whose namespace does not follow App\\Models\\...\\X => App\\Http\\Resources\\...\\XResource convention, with no UseResource PHP attribute set on the model class.","commonSituations":"Models in non-standard namespaces (no \\Models\\ segment), missing or differently-named resource classes, newly added models without a corresponding resource, or calling toResource() on a model from a package.","solutions":["Pass the resource class explicitly: $model->toResource(UserResource::class).","Create the conventional resource class (e.g. App\\Http\\Resources\\UserResource) matching the model's namespace.","Annotate the model with #[UseResource(UserResource::class)] so the guesser resolves it deterministically."],"exampleFix":"// before\nreturn $user->toResource();\n\n// after\nreturn $user->toResource(UserResource::class);\n// or attribute on the model:\n#[\\Illuminate\\Database\\Eloquent\\Attributes\\UseResource(UserResource::class)]\nclass User extends Model {}","handlingStrategy":"validation","validationCode":"$guessed = array_filter(get_class($model)::guessResourceName(), 'class_exists');\nif (empty($guessed)) {\n    throw new \\LogicException('No resource for '.get_class($model).'; pass one explicitly.');\n}\n$model->toResource();","typeGuard":"function hasResolvableResource(string $modelClass): bool {\n    if (! empty(array_filter($modelClass::guessResourceName(), 'class_exists'))) {\n        return true;\n    }\n    return (new \\ReflectionClass($modelClass))\n        ->getAttributes(\\Illuminate\\Database\\Eloquent\\Attributes\\UseResource::class) !== [];\n}","tryCatchPattern":"try {\n    return $model->toResource();\n} catch (\\LogicException $e) {\n    if (str_starts_with($e->getMessage(), 'Failed to find resource class')) {\n        return $model->toResource(\\App\\Http\\Resources\\JsonResource::class);\n    }\n    throw $e;\n}","preventionTips":["Pass the resource class explicitly when convention is uncertain.","Add #[UseResource(...)] attribute to the model for deterministic resolution.","Keep resource class names aligned to the App\\Http\\Resources\\<Model>Resource convention."],"tags":["eloquent","api-resources","convention","reflection"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}