Select createOptionForm not saved with the record

I have the following code which it works in v2 but not after upgrade to v3. The category saved and appeared in the select, but when save the form it's not saved with the created model.
Forms\Components\Select::make('category_id')
->label(__('validation.attributes.category'))
->relationship('category', 'name', fn ($query) => $query->with('parent'))
->getOptionLabelFromRecordUsing(fn ($record) => collect([$record->parent?->name, $record->name])->filter()->join(' - '))
->required()
->searchable()
->preload()
->createOptionForm([
Forms\Components\TextInput::make('name')
->label(__('validation.attributes.name'))
->autocomplete(false)
->required()
->rule(fn ($livewire) => UniqueJsonRule::for(table: 'shop_categories', column: 'name', locale: $livewire->activeLocale)),
Forms\Components\Select::make('parent_id')
->label(__('validation.attributes.parent_category'))
->placeholder(__('Leave it empty to be added as a parent category'))
->relationship('parent', 'name', fn (Builder $query) => $query->where('parent_id', null))
->getOptionLabelFromRecordUsing(fn ($record) => $record->name)
->searchable()
->preload(),
])
->createOptionUsing(function ($component, $data, $form) {
$record = $component->getRelationship()->getRelated();
$record->fill($data);
$record->save();

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

return $record->global_id;
})
Forms\Components\Select::make('category_id')
->label(__('validation.attributes.category'))
->relationship('category', 'name', fn ($query) => $query->with('parent'))
->getOptionLabelFromRecordUsing(fn ($record) => collect([$record->parent?->name, $record->name])->filter()->join(' - '))
->required()
->searchable()
->preload()
->createOptionForm([
Forms\Components\TextInput::make('name')
->label(__('validation.attributes.name'))
->autocomplete(false)
->required()
->rule(fn ($livewire) => UniqueJsonRule::for(table: 'shop_categories', column: 'name', locale: $livewire->activeLocale)),
Forms\Components\Select::make('parent_id')
->label(__('validation.attributes.parent_category'))
->placeholder(__('Leave it empty to be added as a parent category'))
->relationship('parent', 'name', fn (Builder $query) => $query->where('parent_id', null))
->getOptionLabelFromRecordUsing(fn ($record) => $record->name)
->searchable()
->preload(),
])
->createOptionUsing(function ($component, $data, $form) {
$record = $component->getRelationship()->getRelated();
$record->fill($data);
$record->save();

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

return $record->global_id;
})
7 Replies
Wiebe
Wiebe9mo ago
I've got it the other way around, after creating it in the modal its not visible in the select but when saving its connected.
MohamedSabil83
MohamedSabil839mo ago
Can't find the reason. For now, I add the category_id to the Model $fillable until I have a time to dig it more
cheesegrits
cheesegrits9mo ago
Without category_id in $fillable, do your category selections save if you are selecting an existing one rather than creating a new one?
MohamedSabil83
MohamedSabil839mo ago
Yes, the existing categories saved. The new one created and appeared in the select but not saved. You need to edit the record and re-select it. Also, I guess it will return errors if my category_id not nullable
cheesegrits
cheesegrits9mo ago
And is global_id the primary key of the model?
MohamedSabil83
MohamedSabil839mo ago
No, it's not
cheesegrits
cheesegrits9mo ago
OK, the createOptionUsing() method has to return the key of the created model, I guess it doesn't have to be the PK, although 99 times out of 100 it would be. But it needs to be whatever your Select relationship ('category') is using as the key.