{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/Traits/TransformsToResourceCollection.php#L29-L65","documentation":"Thrown by guessResourceCollection() when the first item in the collection is not an object. The guesser inspects the first item's class to derive a resource class, so scalar/array/null items cannot be guessed. LogicException is thrown. An empty collection short-circuits earlier and does not trigger this.","triggerScenarios":"Calling $collection->toResourceCollection() on a collection of arrays (e.g. DB query builder results without ->get() model hydration, or array maps of scalars). Also mixing plain arrays into an Eloquent collection.","commonSituations":"Calling toResourceCollection() on a collection built from raw arrays or stdClass instead of Eloquent models. Decoding JSON to arrays and then treating the result as a model collection.","solutions":["Ensure the collection contains model instances; hydrate via Model::hydrate($array) or re-query.","Pass an explicit resource class: toResourceCollection(UserResource::class).","Convert scalar items to objects before calling, or build the resource manually."],"exampleFix":"// before\nreturn $collection->toResourceCollection(); // $collection holds arrays\n\n// after\nreturn $collection->toResourceCollection(UserResource::class);","handlingStrategy":"validation","validationCode":"if (! $collection->isEmpty() && ! is_object($collection->first())) {\n    // hydrate or pass explicit resource class\n    return $collection->toResourceCollection(UserResource::class);\n}\nreturn $collection->toResourceCollection();","typeGuard":"function allObjects(\\Illuminate\\Support\\Collection $c): bool {\n    return $c->every(fn ($i) => is_object($i));\n}","tryCatchPattern":"try {\n    $resource = $collection->toResourceCollection();\n} catch (\\LogicException $e) {\n    $resource = $collection->toResourceCollection(UserResource::class);\n}","preventionTips":["Keep Eloquent collections holding model instances; avoid mixing arrays/stdClass.","Always pass an explicit resource class when the collection origin is uncertain."],"tags":["laravel","collections","resources","api-resources","type-mismatch"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}