© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
1 reply
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() {
... ?
}), 
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Editing pivot table relating 3 tables
FilamentFFilament / ❓┊help
3y ago
pivot table export
FilamentFFilament / ❓┊help
2y ago
Show Pivot Table
FilamentFFilament / ❓┊help
3y ago
Repeater with pivot table
FilamentFFilament / ❓┊help
11mo ago