Dependant select with multiple options

I have 2 select where I can select several options and both are many to many with a polymorphic relationship. Right now the second does not depend on the first and it is what I need. Would it be possible? Actual code:
Select::make('nationable')
->relationship(name: 'nations', titleAttribute: 'name')
->searchable()
->preload()
->multiple()
->required(),

Select::make('champable')
->relationship(name: 'championships', titleAttribute: 'name')
->searchable()
->preload()
->multiple()
->required(),
Select::make('nationable')
->relationship(name: 'nations', titleAttribute: 'name')
->searchable()
->preload()
->multiple()
->required(),

Select::make('champable')
->relationship(name: 'championships', titleAttribute: 'name')
->searchable()
->preload()
->multiple()
->required(),
8 Replies
H.Bilbao
H.BilbaoOP16mo ago
thanks @N1XN I test it but I can't do it work with multiple morph relation. I test with new v3 documentation to but the same:
N1XN
N1XN16mo ago
Try removing 'name:' and 'titleAttribute:' inside relationships() function. It should be ->relationships('nations', 'name') Ultimately you want MorphToSelect https://filamentphp.com/docs/3.x/forms/fields/select#handling-morphto-relationships
cheesegrits
cheesegrits16mo ago
FYI, that's just named arguments, which got added in PHP 8.0. And those are the correct argument names. The Filament core has been steadily moving over to using named arguments for a while, and a lot of the documentation is starting to use them ... including the Select relationship() doc ... https://filamentphp.com/docs/3.x/forms/fields/select#integrating-with-an-eloquent-relationship
N1XN
N1XN16mo ago
Oh great! Thanks for this detailed explanation!
cheesegrits
cheesegrits16mo ago
It's very useful for skipping arguments. Before if you had a function with (say) four args, and you only wanted to change the last one and just take the defaults for the rest, you had to provide all four, like someFunction(123, 'foo', 'bar', $thisOne). Now you can just name the one you want to change, like someFunction(paramName: $thisOne).
H.Bilbao
H.BilbaoOP16mo ago
Thanks to all, I solved with these:
Select::make('nationable')
->relationship(name: 'nations', titleAttribute: 'name')
->searchable()
->multiple()
->required()
->live(),

Select::make('champable')
->options(function (Get $get) {
$champs = Champ::whereHas('nations', function ($subquery) use ($get) {
$subquery->whereIn('nation_id', $get('nationable'));
});
if ($champs->doesntExist()) {
return [];
}
else {
return $champs->pluck('name', 'id');}
})
->searchable()
->multiple()
->required(),
Select::make('nationable')
->relationship(name: 'nations', titleAttribute: 'name')
->searchable()
->multiple()
->required()
->live(),

Select::make('champable')
->options(function (Get $get) {
$champs = Champ::whereHas('nations', function ($subquery) use ($get) {
$subquery->whereIn('nation_id', $get('nationable'));
});
if ($champs->doesntExist()) {
return [];
}
else {
return $champs->pluck('name', 'id');}
})
->searchable()
->multiple()
->required(),
How could I do, for example, if I delete a value in the first select so that the related values are removed from the dependent select?
Want results from more Discord servers?
Add your server