filamentphp/filament · error · InvalidArgumentException

MorphToSelect of class [$morphToSelectClass] must have a uni

Error message

MorphToSelect of class [$morphToSelectClass] must have a unique name, passed to the [make()] method.

What it means

MorphToSelect's name is also the state path and the key under which the morph type/id pair is stored, so it must be unique and non-blank. `make()` falls back to `getDefaultName()` (null on the base class) and throws an InvalidArgumentException when the name is still blank after the fallback.

Source

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

    protected ?Closure $modifyTypeSelectUsing = null;

    protected ?Closure $modifyKeySelectUsing = null;

    protected bool | Closure $hasTypeSelectToggleButtons = false;

    final public function __construct(string $name)
    {
        $this->name($name);
    }

    public static function make(?string $name = null): static
    {
        $morphToSelectClass = static::class;

        $name ??= static::getDefaultName();

        if (blank($name)) {
            throw new InvalidArgumentException("MorphToSelect of class [$morphToSelectClass] must have a unique name, passed to the [make()] method.");
        }

        $static = app($morphToSelectClass, ['name' => $name]);
        $static->configure();

        return $static;
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->schema(function (MorphToSelect $component): array {
            $relationship = $component->getRelationship();
            $typeColumn = $relationship->getMorphType();
            $keyColumn = $relationship->getForeignKeyName();

            $types = $component->getTypes();

View on GitHub (pinned to 53483fa934)

Solutions

  1. Pass an explicit name matching the morph relation: `MorphToSelect::make('commentable')`.
  2. Override `getDefaultName()` in custom subclasses to return a stable identifier.
  3. Guard dynamic schema builders against null names.

Example fix

// before
MorphToSelect::make()
    ->types([...]),

// after
MorphToSelect::make('commentable')
    ->types([...]),
Defensive patterns

Strategy: type-guard

Validate before calling

foreach ($morphFields as $name => $definition) {
    if (blank($name)) {
        continue;
    }

    $fields[] = MorphToSelect::make($name)->types($definition['types']);
}

Type guard

function assertMorphToSelectName(?string $name): string
{
    if (blank($name)) {
        throw new InvalidArgumentException('MorphToSelect name cannot be blank.');
    }

    return $name;
}

// MorphToSelect::make(assertMorphToSelectName($name))

Prevention

When it happens

Trigger: `MorphToSelect::make()` with no argument, or `MorphToSelect::make($name)` where `$name` is null/empty when the form schema is built.

Common situations: Custom morph-select subclasses without a `getDefaultName()` override, or schema generation from metadata where the key is missing.

Related errors


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