Relationship not saved when using createOptionUsing on Select input

Hi guys, I am trying to create subject via select input with createOptionForm. Inside that form is another relationship (seller), that is not saved when I chain createOptionUsing method. When I remove that method seller relationship is saved correctly, but I also need to save broker_id (user) to that subject, thats why I use createOptionUsing method. Can you help me?
Forms\Components\Select::make('subject_id')
->label(__('Subject'))
->relationship('subject', 'name')
->createOptionForm(static::getSubjectFields())
->createOptionUsing(function (array $data, Forms\Get $get): int {
$user = User::find($get('broker_id')) ?? auth()->user();

return $user->subjects()->create($data)->getKey();
})
->saveRelationshipsUsing(function (Select $component, Model $record, $state) {
dump($component, $record, $state);
})
->saveRelationshipsBeforeChildrenUsing(function (Select $component, Model $record, $state) {
dump($component, $record, $state);
}),
Forms\Components\Select::make('subject_id')
->label(__('Subject'))
->relationship('subject', 'name')
->createOptionForm(static::getSubjectFields())
->createOptionUsing(function (array $data, Forms\Get $get): int {
$user = User::find($get('broker_id')) ?? auth()->user();

return $user->subjects()->create($data)->getKey();
})
->saveRelationshipsUsing(function (Select $component, Model $record, $state) {
dump($component, $record, $state);
})
->saveRelationshipsBeforeChildrenUsing(function (Select $component, Model $record, $state) {
dump($component, $record, $state);
}),
1 Reply
Trauma Zombie
Trauma ZombieOP8mo ago
I updated that method to this:
->createOptionUsing(function (Select $component, array $data, Form $form, Forms\Get $get): int {
$record = $component->getRelationship()->getRelated();
$record->fill([
...$data,
'broker_id' => User::find($get('broker_id'))->id ?? auth()->id(),
]);
$record->save();

$form->model($record)->saveRelationships();

return $record->getKey();
}),
->createOptionUsing(function (Select $component, array $data, Form $form, Forms\Get $get): int {
$record = $component->getRelationship()->getRelated();
$record->fill([
...$data,
'broker_id' => User::find($get('broker_id'))->id ?? auth()->id(),
]);
$record->save();

$form->model($record)->saveRelationships();

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