© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
1 reply
1benjam1

Unique rule with MorphToSelect fields

Hi ! I'm trying to create a form for a
"relations"
"relations"
polymorphic table I have in my database to manage relations between three entities (Users, Companies and Institutions). Here is the table code :
  Schema::create(self::TABLE_NAME, function (Blueprint $table): void {
      $table->id();
      $table->bigInteger('relationable_id')->unsigned();
      $table->string('relationable_type');
      $table->bigInteger('target_id')->unsigned();
      $table->string('target_type');
      $table->string('relation_type', 45);
      $table->timestamps();
      $table->index('relation_type');
      $table->unique(['relationable_id', 'relationable_type', 'target_type', 'relation_type'], self::TABLE_NAME . '_unique');
  });
  Schema::create(self::TABLE_NAME, function (Blueprint $table): void {
      $table->id();
      $table->bigInteger('relationable_id')->unsigned();
      $table->string('relationable_type');
      $table->bigInteger('target_id')->unsigned();
      $table->string('target_type');
      $table->string('relation_type', 45);
      $table->timestamps();
      $table->index('relation_type');
      $table->unique(['relationable_id', 'relationable_type', 'target_type', 'relation_type'], self::TABLE_NAME . '_unique');
  });


I already used
->unique()
->unique()
method on classic Select fields like that :
Forms\Components\Select::make('user_id')
->relationship('user', 'v_fullname')
->unique(modifyRuleUsing: function (Unique $rule) {
    return $rule
        ->where('model_type', Company::class)
        ->where('model_id', $this->getOwnerRecord());
    })
Forms\Components\Select::make('user_id')
->relationship('user', 'v_fullname')
->unique(modifyRuleUsing: function (Unique $rule) {
    return $rule
        ->where('model_type', Company::class)
        ->where('model_id', $this->getOwnerRecord());
    })


But is there any possibility define a
unique()
unique()
rule on
MorphToSelect
MorphToSelect
fields ?

Forms\Components\MorphToSelect::make('relationable')
  ->label('Relationable')
  ->types([
      Forms\Components\MorphToSelect\Type::make(Company::class)
          ->titleAttribute('name'),
      Forms\Components\MorphToSelect\Type::make(Institution::class)
          ->titleAttribute('name'),
  ]),
Forms\Components\MorphToSelect::make('relationable')
  ->label('Relationable')
  ->types([
      Forms\Components\MorphToSelect\Type::make(Company::class)
          ->titleAttribute('name'),
      Forms\Components\MorphToSelect\Type::make(Institution::class)
          ->titleAttribute('name'),
  ]),


Thanks in advance !
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Need help with unique rule and ignore
FilamentFFilament / ❓┊help
3y ago
MorphToSelect with createOptionForm
FilamentFFilament / ❓┊help
3y ago
MorphToSelect with allowHtml
FilamentFFilament / ❓┊help
3y ago
Customizing the Unique validation rule
FilamentFFilament / ❓┊help
3y ago