How to set value to createOptionForm from another field?

Hi guys, I need a little help from you. This is part of my contract form. I have two selects: broker and client. For client select I have createOptionForm for modal creation of client. When I set broker_id in first select I want to use it as broker_id in the createOptionForm. I dont know how. Right now, it will probably get value from the same hidden input. It is null on fill, so it gets null.
// ContractResource form()

Forms\Components\Select::make('broker_id') // this is broker_id on contract model
->label(__('Broker'))
->options(User::pluck('name', 'id'))
->default(auth()->id())
->dehydrated()
->live(),

Forms\Components\Select::make('client_id')
->label(__('Client'))
->relationship('client')
->createOptionForm([
Forms\Components\Hidden::make('broker_id') // this is broker_id on client model
->dehydrateStateUsing(fn (null $state, Forms\Get $get) => $get('broker_id')),

Forms\Components\Group::make()
->schema([
PhoneInput::make()
->required(),

EmailInput::make()
->required(),
]),
])
->createOptionModalHeading(__('Create new client')),
// ContractResource form()

Forms\Components\Select::make('broker_id') // this is broker_id on contract model
->label(__('Broker'))
->options(User::pluck('name', 'id'))
->default(auth()->id())
->dehydrated()
->live(),

Forms\Components\Select::make('client_id')
->label(__('Client'))
->relationship('client')
->createOptionForm([
Forms\Components\Hidden::make('broker_id') // this is broker_id on client model
->dehydrateStateUsing(fn (null $state, Forms\Get $get) => $get('broker_id')),

Forms\Components\Group::make()
->schema([
PhoneInput::make()
->required(),

EmailInput::make()
->required(),
]),
])
->createOptionModalHeading(__('Create new client')),
1 Reply
Trauma Zombie
Trauma ZombieOP9mo ago
I found this method that fixed my problem:
->createOptionUsing(function (array $data, Forms\Get $get): int {
$user = User::find($get('broker_id')) ?? auth()->user();

return $user->subjects()->create($data)->getKey();
}),
->createOptionUsing(function (array $data, Forms\Get $get): int {
$user = User::find($get('broker_id')) ?? auth()->user();

return $user->subjects()->create($data)->getKey();
}),
Want results from more Discord servers?
Add your server