{"record":{"id":"66dc42ecaedb180c","repo":"filamentphp/filament","slug":"the-relationship-relationshipname-does-not-ex-66dc42","errorCode":null,"errorMessage":"The relationship [{$relationshipName}] does not exist on the model [{$this->getModel()}].","messagePattern":"The relationship \\[(.+?)\\] does not exist on the model \\[(.+?)\\]\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"packages/forms/src/Components/MorphToSelect.php","lineNumber":240,"sourceCode":"\n    /**\n     * @param  array<Type> | Closure  $types\n     */\n    public function types(array | Closure $types): static\n    {\n        $this->types = $types;\n\n        return $this;\n    }\n\n    public function getRelationship(): MorphTo\n    {\n        $record = $this->getModelInstance();\n\n        $relationshipName = $this->getName();\n\n        if ($record->hasAttribute($relationshipName) || (! $record->isRelation($relationshipName))) {\n            throw new LogicException(\"The relationship [{$relationshipName}] does not exist on the model [{$this->getModel()}].\");\n        }\n\n        return $record->{$relationshipName}();\n    }\n\n    /**\n     * @return array<string, Type>\n     */\n    public function getTypes(): array\n    {\n        $types = [];\n\n        foreach ($this->evaluate($this->types) as $type) {\n            $types[$type->getAlias()] = $type;\n        }\n\n        return $types;\n    }","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/filamentphp/filament/blob/53483fa9340e4ec637985fe454ebb58afb92ea24/packages/forms/src/Components/MorphToSelect.php#L222-L258","documentation":"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`.","triggerScenarios":"The field name is a column like `commentable_type` or `subject_id`, or the model simply lacks the `morphTo()` method with that name.","commonSituations":"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.","solutions":["Name the field after the relation method: `MorphToSelect::make('commentable')`.","Define the relation on the model: `public function commentable(): MorphTo { return $this->morphTo(); }`."],"exampleFix":"// before — field named after the column, no relation defined\nMorphToSelect::make('commentable_type'),\n\n// after — app/Models/Comment.php\npublic function commentable(): MorphTo\n{\n    return $this->morphTo();\n}\n\n// schema\nMorphToSelect::make('commentable'),","handlingStrategy":"validation","validationCode":"// Preflight: the field name must be a morph relation on the model\n$instance = $modelClass::newInstance();\n\nif ($instance->hasAttribute($fieldName) || (! $instance->isRelation($fieldName))) {\n    throw new InvalidArgumentException(\n        \"[{$fieldName}] must be a morphTo() relation method on [{$modelClass}].\"\n    );\n}","typeGuard":"function modelHasMorphRelation(string $modelClass, string $name): bool\n{\n    $instance = $modelClass::newInstance();\n\n    if ($instance->hasAttribute($name) || (! $instance->isRelation($name))) {\n        return false;\n    }\n\n    return $instance->{$name}() instanceof \\Illuminate\\Database\\Eloquent\\Relations\\MorphTo;\n}","tryCatchPattern":null,"preventionTips":["Name the field after the relation method, not the *_type/*_id columns.","Define the morphTo() on the model before adding the field.","Cover morph fields with a schema lint that runs hasAttribute()/isRelation() checks."],"tags":["filament","forms","morph-to-select","eloquent","relationships"],"backgroundTag":"missing-eloquent-relationship","analyzedSha":"53483fa9340e4ec637985fe454ebb58afb92ea24","analyzedAt":"2026-08-17T02:06:23.049Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}