Unique rule with MorphToSelect fields

Hi ! I'm trying to create a form for a "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() 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() rule on 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 !
0 Replies
No replies yetBe the first to reply to this messageJoin