{"record":{"id":"f370bd00c59d1296","repo":"laravel/framework","slug":"resource-collection-guesser-expects-the-collection","errorCode":null,"errorMessage":"Resource collection guesser expects the collection to contain objects.","messagePattern":"Resource collection guesser expects the collection to contain objects\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Traits/TransformsToResourceCollection.php","lineNumber":47,"sourceCode":"    }\n\n    /**\n     * Guess the resource collection for the items.\n     *\n     * @return \\Illuminate\\Http\\Resources\\Json\\ResourceCollection\n     *\n     * @throws \\Throwable\n     */\n    protected function guessResourceCollection(): ResourceCollection\n    {\n        if ($this->isEmpty()) {\n            return new ResourceCollection($this);\n        }\n\n        $model = $this->items[0] ?? null;\n\n        if (! is_object($model)) {\n            throw new LogicException('Resource collection guesser expects the collection to contain objects.');\n        }\n\n        /** @var class-string<Model> $className */\n        $className = get_class($model);\n\n        if (! method_exists($className, 'guessResourceName')) {\n            throw new LogicException(sprintf('Expected class %s to implement guessResourceName method. Make sure the model uses the TransformsToResource trait.', $className));\n        }\n\n        $useResourceCollection = $this->resolveResourceCollectionFromAttribute($className);\n\n        if ($useResourceCollection !== null && class_exists($useResourceCollection)) {\n            return new $useResourceCollection($this);\n        }\n\n        $useResource = $this->resolveResourceFromAttribute($className);\n\n        if ($useResource !== null && class_exists($useResource)) {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Traits/TransformsToResourceCollection.php#L29-L65","documentation":"Thrown by guessResourceCollection() when the collection is non-empty but its first element ($this->items[0]) is not an object. The guesser inspects the first item to derive the model class and resolve its API resource; scalar/array/null items have no class to introspect, so it throws LogicException. An empty collection short-circuits earlier and is fine.","triggerScenarios":"Calling ->toResourceCollection() (with no explicit class) on a collection of scalars/arrays/strings; a collection of stdObjects that was actually built from arrays; a query that returned plain arrays instead of models.","commonSituations":"Selecting raw DB rows as arrays, mixing map-then-resource flows, calling the resource helper on a non-Eloquent dataset, or a collection that lost its object nature through an early array cast.","solutions":["Pass an explicit resource class to toResourceCollection(MyResource::class).","Ensure the collection contains model objects (use Eloquent, not raw arrays).","Cast the items back to objects if appropriate, or use a manual resource mapping."],"exampleFix":"// before\n$resource = $scalarCollection->toResourceCollection();\n// after\n$resource = $scalarCollection->toResourceCollection(MyResource::class);","handlingStrategy":"type-guard","validationCode":"if (! $collection->isEmpty() && ! is_object($collection->first())) {\n    throw new \\LogicException('Collection must contain objects for resource guessing.');\n}\nreturn $collection->toResourceCollection();","typeGuard":"function isObjectCollection(\\Illuminate\\Support\\Collection $c): bool {\n    return $c->isEmpty() || $c->every(fn ($i) => is_object($i));\n}","tryCatchPattern":"try {\n    return $collection->toResourceCollection();\n} catch (\\LogicException $e) {\n    return $collection->toResourceCollection(\\App\\Http\\Resources\\GenericResource::class);\n}","preventionTips":["Keep collections as Eloquent results rather than raw arrays.","Pass an explicit resource class when the source type is uncertain.","Add an explicit class-string argument to avoid guess reliance."],"tags":["api-resources","collections","eloquent","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}