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()
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(),
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
2 Replies
CGM
CGM2w ago
Is this related? https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#passing-the-form-model-after-the-form-has-been-submitted Not sure if that's what you're looking for, but I just ran across it in the doc's earlier.
use App\Models\Post;

public function create(): void
{
$post = Post::create($this->form->getState());

// Save the relationships from the form to the post after it is created.
$this->form->model($post)->saveRelationships();
}
use App\Models\Post;

public function create(): void
{
$post = Post::create($this->form->getState());

// Save the relationships from the form to the post after it is created.
$this->form->model($post)->saveRelationships();
}
Stormageddon, Dark Lord of All
I don't think it is, I don't think filament can figure out how to handle my relationship if simply run ->saveRelationship() Bumping this up