F
Filament10mo ago
sven

Validate MorphedByMany pivot table

I have a many-to-many polymorphic Author (authorable) relation in my schema. The pivot table also has a role_id that links to an author_roles table. In my ArticleResource I now built a relation manager between the Article model and Author relation. The idea is that users can attach Authors and specify the AuthorRole in an AttachAction on the relation manager. It's possible to add an author to the same record multiple times, but not with the same role. (eg. The author could be both an writer and editor of the article.) I now want to write a Unique validation rule to check that the author_id, authorable_type, authorable_id, AND author_role_id are unique. I'm struggling to understand where to get the data and how to access the pivot data to enforce the unique validation rule. Another possibility could be not to show the duplicate option in the first place, or disable it. But I'm not sure how to get the pivot data from Filament This is what I have so far..
(...)->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->recordTitle(function ($record) {
return "{$record->first_name} {$record->last_name}";
})
->recordSelect(
fn (Select $select) => $select
->placeholder('Select an author')
)
->recordSelectSearchColumns(['first_name', 'last_name'])
->form(fn (Tables\Actions\AttachAction $action, RelationManager $livewire, $record): array => [
$action->getRecordSelect(),
Forms\Components\Select::make('author_role_id')
->preload()
->options(
AuthorRole::pluck('role_name', 'id')->toArray()
)
->searchable()
->required()
->label('Role')
])->label('Add existing')->unique(modifyQueryUsing: function() {
... ?
}),
(...)->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->recordTitle(function ($record) {
return "{$record->first_name} {$record->last_name}";
})
->recordSelect(
fn (Select $select) => $select
->placeholder('Select an author')
)
->recordSelectSearchColumns(['first_name', 'last_name'])
->form(fn (Tables\Actions\AttachAction $action, RelationManager $livewire, $record): array => [
$action->getRecordSelect(),
Forms\Components\Select::make('author_role_id')
->preload()
->options(
AuthorRole::pluck('role_name', 'id')->toArray()
)
->searchable()
->required()
->label('Role')
])->label('Add existing')->unique(modifyQueryUsing: function() {
... ?
}),
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server