filamentphp/filament · error · LogicException

The relationship [{$relationshipName}] does not exist on the

Error message

The relationship [{$relationshipName}] does not exist on the model [{$this->getModel()}].

What it means

MorphToSelect uses the field's own name as the polymorphic relation to call on the model (`MorphToSelect::make('commentable')` calls `commentable()`). `getRelationship()` verifies the name is a relation and not a plain attribute (e.g. `commentable_type`) before invoking it; otherwise it throws this LogicException, since calling the method would not return a `MorphTo`.

Source

Thrown at packages/forms/src/Components/MorphToSelect.php:240

    /**
     * @param  array<Type> | Closure  $types
     */
    public function types(array | Closure $types): static
    {
        $this->types = $types;

        return $this;
    }

    public function getRelationship(): MorphTo
    {
        $record = $this->getModelInstance();

        $relationshipName = $this->getName();

        if ($record->hasAttribute($relationshipName) || (! $record->isRelation($relationshipName))) {
            throw new LogicException("The relationship [{$relationshipName}] does not exist on the model [{$this->getModel()}].");
        }

        return $record->{$relationshipName}();
    }

    /**
     * @return array<string, Type>
     */
    public function getTypes(): array
    {
        $types = [];

        foreach ($this->evaluate($this->types) as $type) {
            $types[$type->getAlias()] = $type;
        }

        return $types;
    }

View on GitHub (pinned to 53483fa934)

Solutions

  1. Name the field after the relation method: `MorphToSelect::make('commentable')`.
  2. Define the relation on the model: `public function commentable(): MorphTo { return $this->morphTo(); }`.

Example fix

// before — field named after the column, no relation defined
MorphToSelect::make('commentable_type'),

// after — app/Models/Comment.php
public function commentable(): MorphTo
{
    return $this->morphTo();
}

// schema
MorphToSelect::make('commentable'),
Defensive patterns

Strategy: validation

Validate before calling

// Preflight: the field name must be a morph relation on the model
$instance = $modelClass::newInstance();

if ($instance->hasAttribute($fieldName) || (! $instance->isRelation($fieldName))) {
    throw new InvalidArgumentException(
        "[{$fieldName}] must be a morphTo() relation method on [{$modelClass}]."
    );
}

Type guard

function modelHasMorphRelation(string $modelClass, string $name): bool
{
    $instance = $modelClass::newInstance();

    if ($instance->hasAttribute($name) || (! $instance->isRelation($name))) {
        return false;
    }

    return $instance->{$name}() instanceof \Illuminate\Database\Eloquent\Relations\MorphTo;
}

Prevention

When it happens

Trigger: The field name is a column like `commentable_type` or `subject_id`, or the model simply lacks the `morphTo()` method with that name.

Common situations: Naming the field after the foreign-key columns instead of the relation method, or the `morphTo()` was never added to the model / was renamed during a refactor.

Related errors


AI-assisted analysis of filamentphp/filament@53483fa934 (2026-08-17). Data as JSON: /api/errors/66dc42ecaedb180c. Report an issue: GitHub.