laravel/framework · error · LogicException

Failed to find resource class for model [%s].

Error message

Failed to find resource class for model [%s].

What it means

Thrown by TransformsToResource::guessResource() when Model::toResource() is called without an explicit class and the framework cannot find a matching resource. The guesser first checks a UseResource attribute on the model, then tries convention-based names derived from the model's namespace (replacing \Models\ with \Http\Resources\ and appending 'Resource'). If none of those classes exist, it throws this LogicException.

Source

Thrown at src/Illuminate/Database/Eloquent/Concerns/TransformsToResource.php:49

     * @return \Illuminate\Http\Resources\Json\JsonResource
     *
     * @throws \LogicException
     */
    protected function guessResource(): JsonResource
    {
        $resourceClass = $this->resolveResourceFromAttribute(static::class);

        if ($resourceClass !== null && class_exists($resourceClass)) {
            return $resourceClass::make($this);
        }

        foreach (static::guessResourceName() as $resourceClass) {
            if (is_string($resourceClass) && class_exists($resourceClass)) {
                return $resourceClass::make($this);
            }
        }

        throw new LogicException(sprintf('Failed to find resource class for model [%s].', get_class($this)));
    }

    /**
     * Guess the resource class name for the model.
     *
     * @return array{class-string<\Illuminate\Http\Resources\Json\JsonResource>, class-string<\Illuminate\Http\Resources\Json\JsonResource>}
     */
    public static function guessResourceName(): array
    {
        $modelClass = static::class;

        if (! Str::contains($modelClass, '\\Models\\')) {
            return [];
        }

        $relativeNamespace = Str::after($modelClass, '\\Models\\');

        $relativeNamespace = Str::contains($relativeNamespace, '\\')

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Pass the resource class explicitly: $model->toResource(UserResource::class).
  2. Create the conventional resource class (e.g. App\Http\Resources\UserResource) matching the model's namespace.
  3. Annotate the model with #[UseResource(UserResource::class)] so the guesser resolves it deterministically.

Example fix

// before
return $user->toResource();

// after
return $user->toResource(UserResource::class);
// or attribute on the model:
#[\Illuminate\Database\Eloquent\Attributes\UseResource(UserResource::class)]
class User extends Model {}
Defensive patterns

Strategy: validation

Validate before calling

$guessed = array_filter(get_class($model)::guessResourceName(), 'class_exists');
if (empty($guessed)) {
    throw new \LogicException('No resource for '.get_class($model).'; pass one explicitly.');
}
$model->toResource();

Type guard

function hasResolvableResource(string $modelClass): bool {
    if (! empty(array_filter($modelClass::guessResourceName(), 'class_exists'))) {
        return true;
    }
    return (new \ReflectionClass($modelClass))
        ->getAttributes(\Illuminate\Database\Eloquent\Attributes\UseResource::class) !== [];
}

Try / catch

try {
    return $model->toResource();
} catch (\LogicException $e) {
    if (str_starts_with($e->getMessage(), 'Failed to find resource class')) {
        return $model->toResource(\App\Http\Resources\JsonResource::class);
    }
    throw $e;
}

Prevention

When it happens

Trigger: Calling $model->toResource() (or a route/response that calls it) on a model whose namespace does not follow App\Models\...\X => App\Http\Resources\...\XResource convention, with no UseResource PHP attribute set on the model class.

Common situations: Models in non-standard namespaces (no \Models\ segment), missing or differently-named resource classes, newly added models without a corresponding resource, or calling toResource() on a model from a package.

Related errors


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