{"record":{"id":"4f645619f3074556","repo":"laravel/framework","slug":"failed-to-find-resource-class-for-model-s","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/Collections/Traits/TransformsToResourceCollection.php","lineNumber":85,"sourceCode":"        }\n\n        $resourceClasses = $className::guessResourceName();\n\n        foreach ($resourceClasses as $resourceClass) {\n            $resourceCollection = $resourceClass.'Collection';\n\n            if (is_string($resourceCollection) && class_exists($resourceCollection)) {\n                return new $resourceCollection($this);\n            }\n        }\n\n        foreach ($resourceClasses as $resourceClass) {\n            if (is_string($resourceClass) && class_exists($resourceClass)) {\n                return $resourceClass::collection($this);\n            }\n        }\n\n        throw new LogicException(sprintf('Failed to find resource class for model [%s].', $className));\n    }\n\n    /**\n     * Get the resource class from the class attribute.\n     *\n     * @param  class-string<\\Illuminate\\Http\\Resources\\Json\\JsonResource>  $class\n     * @return class-string<*>|null\n     */\n    protected function resolveResourceFromAttribute(string $class): ?string\n    {\n        if (! class_exists($class)) {\n            return null;\n        }\n\n        $attributes = (new ReflectionClass($class))->getAttributes(UseResource::class);\n\n        return $attributes !== []\n            ? $attributes[0]->newInstance()->class","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Traits/TransformsToResourceCollection.php#L67-L103","documentation":"Thrown by guessResourceCollection() as the final fallback when no resolvable resource class was found. After checking the UseResourceCollection attribute, the UseResource attribute, and the class::guessResourceName() list (appending 'Collection' and testing class_exists on each), if none of the candidate classes exist it throws LogicException naming the model class.","triggerScenarios":"Model uses TransformsToResource and guessResourceName returns class names, but none of those classes (or their 'Collection' variants) are autoloadable; a typo in the resource class name returned by guessResourceName; the resource file was deleted/renamed.","commonSituations":"Renaming a resource class without updating guessResourceName; namespace mismatch between the model's guess and the actual resource location; missing composer dumpautoload after adding the resource.","solutions":["Verify the resource class name returned by guessResourceName matches an existing autoloaded class.","Ensure the 'XCollection' variant exists if the guesser tries it, or add a UseResource attribute pointing to a real class.","Pass the resource class explicitly to bypass guessing: ->toResourceCollection(MyResource::class)."],"exampleFix":"// before\n// guessResourceName() returns ['App\\Http\\Resources\\Order'] but class is OrderResource\n// after\nreturn ['App\\Http\\Resources\\OrderResource'];  // correct class name","handlingStrategy":"validation","validationCode":"$first = $collection->first();\n$candidates = $first::guessResourceName();\nforeach ($candidates as $c) {\n    if (class_exists($c) || class_exists($c.'Collection')) {\n        // safe to guess\n    }\n}\nreturn $collection->toResourceCollection();","typeGuard":"function resourceClassResolvable(string $modelClass): bool {\n    if (! method_exists($modelClass, 'guessResourceName')) return false;\n    foreach ($modelClass::guessResourceName() as $r) {\n        if (class_exists($r) || class_exists($r.'Collection')) return true;\n    }\n    return false;\n}","tryCatchPattern":"try {\n    return $collection->toResourceCollection();\n} catch (\\LogicException $e) {\n    return $collection->toResourceCollection(\\App\\Http\\Resources\\DefaultResource::class);\n}","preventionTips":["Keep resource class names in sync with guessResourceName return values.","Run composer dumpautoload after adding/renaming resource classes.","Provide a UseResource attribute as a stable, refactor-safe resolver."],"tags":["api-resources","eloquent","autoloading","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}