laravel/framework · error · LogicException
Expected class to implement guessResourceName method. Make…
Error message
Expected class %s to implement guessResourceName method. Make sure the model uses the TransformsToResource trait.
What it means
Thrown by guessResourceCollection() when the first item is an object but its class does not implement a guessResourceName method and has no UseResource/UseResourceCollection PHP attribute. The guesser needs either the method (provided by the TransformsToResource trait) or a resource attribute to resolve the resource class; without either it throws LogicException naming the offending class.
Solutions
- Add the TransformsToResource trait to the model (provides guessResourceName).
- Add a #[UseResource(MyResource::class)] or #[UseResourceCollection(...)] PHP attribute on the model class.
- Pass the resource class explicitly: ->toResourceCollection(MyResource::class).
Example fix
// before
class Order {
// no trait, no attribute
}
// after
use Illuminate\Database\Eloquent\Attributes\UseResource;
#[UseResource(OrderResource::class)]
class Order extends Model {
use \Illuminate\Support\Traits\TransformsToResource;
} Defensive patterns
Strategy: validation
Validate before calling
$first = $collection->first();
if (is_object($first) && ! method_exists($first, 'guessResourceName') && empty((new \ReflectionClass($first))->getAttributes())) {
// will fail guessing; pass explicit class
}
return $collection->toResourceCollection(\App\Http\Resources\MyResource::class); Type guard
function canGuessResource(object $model): bool {
return method_exists($model, 'guessResourceName')
|| (new \ReflectionClass($model))->getAttributes(\Illuminate\Database\Eloquent\Attributes\UseResource::class) !== []
|| (new \ReflectionClass($model))->getAttributes(\Illuminate\Database\Eloquent\Attributes\UseResourceCollection::class) !== [];
} Try / catch
try {
return $collection->toResourceCollection();
} catch (\LogicException $e) {
return $collection->toResourceCollection(\App\Http\Resources\FallbackResource::class);
} Prevention
- Apply the TransformsToResource trait consistently across models.
- Use #[UseResource] / #[UseResourceCollection] attributes for declarative resolution.
- Pass an explicit resource class in generic code to bypass guessing.
When it happens
Trigger: Calling ->toResourceCollection() on a collection of plain stdClass or a domain object that does not use the TransformsToResource trait and has no UseResource attribute.
Common situations: Migrating to the attribute-based resource resolution and forgetting the trait on a new model; using stdClass/DTOs in a collection and expecting auto resource resolution; partial adoption of the feature across models.
Related errors
- Failed to find resource class for model
- Failed to find resource class for model
- Resource collection guesser expects the collection to…
- A driver must be specified.
- Auth driver [ ] for guard [ ] is not defined.
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/f990945cf1f895ff.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Collections/Traits/TransformsToResourceCollection.php:54
* @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)) {
return $useResource::collection($this);
}
$resourceClasses = $className::guessResourceName();
foreach ($resourceClasses as $resourceClass) {
$resourceCollection = $resourceClass.'Collection';View on GitHub (pinned to e0f6eb3518)