Complex form

Hi! I have a quite complex form at some point, in this form i have a select (brand)

Forms\Components\Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->required()
->preload()
->live()
->afterStateUpdated(fn (Set $set) => $set('item_id', null))
->native(false)
->createOptionForm([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

Forms\Components\Checkbox::make('is_authorized'),
Forms\Components\Hidden::make('team_id')
->default($tenant->id)
])
->editOptionForm([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Checkbox::make('is_authorized'),
Forms\Components\Hidden::make('team_id')
->default($tenant->id)
])
->searchable()
->getSearchResultsUsing(fn (string $search) => Brand::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Brand::find($value)?->name),

Forms\Components\Select::make('brand_id')
->relationship(name: 'brand', titleAttribute: 'name')
->required()
->preload()
->live()
->afterStateUpdated(fn (Set $set) => $set('item_id', null))
->native(false)
->createOptionForm([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

Forms\Components\Checkbox::make('is_authorized'),
Forms\Components\Hidden::make('team_id')
->default($tenant->id)
])
->editOptionForm([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Checkbox::make('is_authorized'),
Forms\Components\Hidden::make('team_id')
->default($tenant->id)
])
->searchable()
->getSearchResultsUsing(fn (string $search) => Brand::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Brand::find($value)?->name),
after i have item (create a new item)
7 Replies
Soundmit
Soundmit7mo ago
as you can see, in the item i have the ability to select an existing item or creating a new one if i choose to create a new item, i have a field to select the related brand. is it possible to automatically select the brand based on the brand select in the first part of the form? I hope I was clear
Soundmit
Soundmit7mo ago
choose brand here
No description
Soundmit
Soundmit7mo ago
and have the brand preselected automatically here
No description
toeknee
toeknee7mo ago
I have a feeling it might be possible but look into the methods o $livewire for say $livewire->parentRecord for instance
Soundmit
Soundmit7mo ago
thanks uhm.. i can't find a solution at the moment
toeknee
toeknee7mo ago
Got it
->createOptionForm(function($livewire) {
$brandId = $livewire->record->brand_id; <- This is where you can grab it.


return [
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

Forms\Components\Checkbox::make('is_authorized'),
Forms\Components\Hidden::make('team_id')
->default($tenant->id)
];
})
->createOptionForm(function($livewire) {
$brandId = $livewire->record->brand_id; <- This is where you can grab it.


return [
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

Forms\Components\Checkbox::make('is_authorized'),
Forms\Components\Hidden::make('team_id')
->default($tenant->id)
];
})
Soundmit
Soundmit7mo ago
uhm nope Attempt to read property "brand_id" on null $brandId = $livewire->record->brand_id; solved with this ->default(fn (Livewire $livewire) => $livewire->data['brand_id'])