© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
3 replies
nowak

User BelongsToMany Groups pivot table with additional "Role" field, how to select role?

I have a
users
users
table and a
groups
groups
table, with a many to many relationship between them, using the group_user pivot table, which contains a
role
role
field, which can be interpreted as a
group_role
group_role
.

I have added the groups field to my UserResource form like this:
public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->label('Username')
                    ->required(),
                Forms\Components\TextInput::make('email')
                    ->email()
                    ->required()
                    ->unique(ignoreRecord: true),
                Forms\Components\TextInput::make('password')
                    ->password()
                    ->required(fn ($livewire) => $livewire instanceof Pages\CreateUser) 
                    ->hidden(fn ($livewire) => $livewire instanceof Pages\EditUser), 
                Forms\Components\TextInput::make('first_name')
                    ->required(),
                Forms\Components\TextInput::make('last_name')
                    ->required(),
                PhoneInput::make('phone_number')
                    ->validateFor()
                    ->defaultCountry('CH')
                    ->required(),
                Forms\Components\TextInput::make('address')
                    ->required(),
                Forms\Components\Select::make('roles')
                    ->relationship('roles', 'name')
                    ->multiple()
                    ->preload(),
                Forms\Components\Select::make('groups')
                    ->relationship('groups', 'name')
                    ->multiple()
                    ->preload(),
            ]);
    }
public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->label('Username')
                    ->required(),
                Forms\Components\TextInput::make('email')
                    ->email()
                    ->required()
                    ->unique(ignoreRecord: true),
                Forms\Components\TextInput::make('password')
                    ->password()
                    ->required(fn ($livewire) => $livewire instanceof Pages\CreateUser) 
                    ->hidden(fn ($livewire) => $livewire instanceof Pages\EditUser), 
                Forms\Components\TextInput::make('first_name')
                    ->required(),
                Forms\Components\TextInput::make('last_name')
                    ->required(),
                PhoneInput::make('phone_number')
                    ->validateFor()
                    ->defaultCountry('CH')
                    ->required(),
                Forms\Components\TextInput::make('address')
                    ->required(),
                Forms\Components\Select::make('roles')
                    ->relationship('roles', 'name')
                    ->multiple()
                    ->preload(),
                Forms\Components\Select::make('groups')
                    ->relationship('groups', 'name')
                    ->multiple()
                    ->preload(),
            ]);
    }

*The roles field in the table is for global user roles, and unrelated to my issue.
I need a way to assign a
group_role
group_role
to my users within each group they belong to, what is the filament way to do this?
Solution
It seemed like the relation manager was the filament way to do this. I added the --attach flag to my php artisan command when creating the relation manager, then removed the create and delete methods so that I would only be able to edit, attach, and detach relations.
For others in the same situation, you can find all information about this here:
https://filamentphp.com/docs/3.x/panels/resources/relation-managers
Managing relationships - Panel Builder - Filament
Jump to solution
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

pivot table user/user
FilamentFFilament / ❓┊help
3y ago
Sorting Forms/Components/Select by Pivot Table Field
FilamentFFilament / ❓┊help
3y ago
Very stuck on BelongsToMany / Pivot table with a restriction
FilamentFFilament / ❓┊help
2y ago
select filter with belongsToMany
FilamentFFilament / ❓┊help
3y ago