F
Filament5mo ago
nowak

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

I have a users table and a groups table, with a many to many relationship between them, using the group_user pivot table, which contains a role field, which can be interpreted as a 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 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...
Jump to solution
1 Reply
Solution
nowak
nowak5mo ago
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