F
Filament15mo ago
pratik

createOptionForm without relationship

I am stuck in this scenario, I have source model that has possible sources, when user is registering the user can select the existing source or create a new one. But as far as I know to use createOptionForm you need to bind it to the relationship. And the problem is since there is no user instance to call relationship on, it's not working. Is there any way to use createOptionForm for Model and not relationship ?
4 Replies
Lara Zeus
Lara Zeus15mo ago
you dont need the relationship use createOptionUsing and createOptionForm
->createOptionUsing(function (array $data) {
//
})
->createOptionUsing(function (array $data) {
//
})
pratik
pratikOP15mo ago
🤯🤯🤯🤯, Thank you @Lara Zeus I didn't find it in the docs.
protected function getReferralSourceFormComponent(): Component
{
return Select::make('source')
->options(ReferSource::pluck('name'))
->getOptionLabelUsing(fn ($value): ?string => ucfirst($value))
->searchable()
->createOptionForm([
TextInput::make('name')
->required(),
])
->createOptionUsing(function (array $data) {
$source = new ReferSource();
$source->name = $data['name'];
$source->save();
return $source->name;
})
->label(__('where did you hear about us ?'))
->required();
}
protected function getReferralSourceFormComponent(): Component
{
return Select::make('source')
->options(ReferSource::pluck('name'))
->getOptionLabelUsing(fn ($value): ?string => ucfirst($value))
->searchable()
->createOptionForm([
TextInput::make('name')
->required(),
])
->createOptionUsing(function (array $data) {
$source = new ReferSource();
$source->name = $data['name'];
$source->save();
return $source->name;
})
->label(__('where did you hear about us ?'))
->required();
}
This is my code, The popup doesn't show when clicking plus button.
Lara Zeus
Lara Zeus15mo ago
are you using panels or forms only? check the dev console for errors and if you're using the forms only make sure to add this to your blade <x-filament-actions::modals />
pratik
pratikOP15mo ago
@Lara Zeus It works now ! Thanks a lot again.
Want results from more Discord servers?
Add your server