laravel/framework · error · LogicException

Resource collection guesser expects the collection to contai

Error message

Resource collection guesser expects the collection to contain objects.

What it means

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.

Source

Thrown at src/Illuminate/Collections/Traits/TransformsToResourceCollection.php:47

    }

    /**
     * Guess the resource collection for the items.
     *
     * @return \Illuminate\Http\Resources\Json\ResourceCollection
     *
     * @throws \Throwable
     */
    protected function guessResourceCollection(): ResourceCollection
    {
        if ($this->isEmpty()) {
            return new ResourceCollection($this);
        }

        $model = $this->items[0] ?? null;

        if (! is_object($model)) {
            throw new LogicException('Resource collection guesser expects the collection to contain objects.');
        }

        /** @var class-string<Model> $className */
        $className = get_class($model);

        if (! method_exists($className, 'guessResourceName')) {
            throw new LogicException(sprintf('Expected class %s to implement guessResourceName method. Make sure the model uses the TransformsToResource trait.', $className));
        }

        $useResourceCollection = $this->resolveResourceCollectionFromAttribute($className);

        if ($useResourceCollection !== null && class_exists($useResourceCollection)) {
            return new $useResourceCollection($this);
        }

        $useResource = $this->resolveResourceFromAttribute($className);

        if ($useResource !== null && class_exists($useResource)) {

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Ensure the collection contains model instances; hydrate via Model::hydrate($array) or re-query.
  2. Pass an explicit resource class: toResourceCollection(UserResource::class).
  3. Convert scalar items to objects before calling, or build the resource manually.

Example fix

// before
return $collection->toResourceCollection(); // $collection holds arrays

// after
return $collection->toResourceCollection(UserResource::class);
Defensive patterns

Strategy: validation

Validate before calling

if (! $collection->isEmpty() && ! is_object($collection->first())) {
    // hydrate or pass explicit resource class
    return $collection->toResourceCollection(UserResource::class);
}
return $collection->toResourceCollection();

Type guard

function allObjects(\Illuminate\Support\Collection $c): bool {
    return $c->every(fn ($i) => is_object($i));
}

Try / catch

try {
    $resource = $collection->toResourceCollection();
} catch (\LogicException $e) {
    $resource = $collection->toResourceCollection(UserResource::class);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/f370bd00c59d1296.json. Report an issue: GitHub.