Manually handling many-to-many relationships

Hi, I have a many to many relationship between my Major and my Course models.

in my Course resource I used to have

Select::make('majors')
   ->relationship('majors', 'name')
   ->multiple()
   ->required()


I wanted to filter the list of majors available in this select based on the value of another column, I came up with this solution

Forms\Components\Select::make('type')
    ->options(InstitutionType::class)
    ->live()
    ->afterStateUpdated(fn (FilamentSet $set) => $set('majors', null) )
    ->required(),
Select::make('majors')
    ->options(fn (Get $get) => Major::where('type', '=', $get('type'))->get()->pluck('name', 'id'))
    ->multiple()
    ->required(),


but without the relationship method the form cannot be submitted properly, because filament no longer knows how to handle my majors and institutions relationship
Was this page helpful?