Select Input populated via db query and setting default option?
Hi there,
I have a select input that is populated by a db query. I am trying to set the default value both at load time and also dynamically when a variable is set but whatever I try to implement this it doesn't work. I am using the "default" method to populate at mount time but no luck. I am not sure how the
Select::make('Customer') ->options(Models\Customer::query() ->orderBy('Company Name') ->pluck('Company Name', 'Customer ID')) ->live() ->default(fn() => "1") ->afterStateUpdated(function ($state) { $this->customer = Models\Customer::find($state); }),'For the dynamic update I am hoping that I can just reference the an object propery and have that update. Any ideas?
7 Replies
Should be
->default(1)
Are you using panel builder and trying it in the CreatePage or using the form builder?Hi @Leonardo Ferreira , I am using it in a form builder. Its a custom page. If I could figure out how to change a selects value programatically I think I could find a way to do what I need to do. Basically I have a form and an page header action. When the page header action button is clicked I want it to change the values in the drop down box based on what a varibale values is.
Did you configure the mount method?
I tried this to test but still can't get the right option selected:
public function mount()
{
$this->customer = Models\Customer::find(1);
}
I think there are 2 issues. Setting a default value with "default" doesn't work as the options array is being loaded asynchronously so the value is not available when "default" fires. Secondly how to change the selected option when the action button is clicked. Please let me know if I am anwsering your question. Maybe I misunderstood.
I have tried '->default(1)' but it hasnt worked.and
$form->statePath('data')
Awesome @Leandro Ferreira A quick test of form->fill() seem to do the trick. Will hack it a bit and work out any wrinkles.