getting parent relationship in form

I have a form that's creating a relationship, but I need to get a reference to the parent record and I can't seem to do it.
Tab::make('config')->label('Config')->schema([
FieldSet::make()->relationship('detail')->schema([
Select::make('sms_type_id')

->options(SmsType::all()->pluck('name', 'id'))

->afterStateUpdated(function (?string $state, ?Model $record) {
$record->sms_type_id = $state;
// $record->location_id = $location->id; <---need to set this parent ID relationship.
$record->save();
}),
Tab::make('config')->label('Config')->schema([
FieldSet::make()->relationship('detail')->schema([
Select::make('sms_type_id')

->options(SmsType::all()->pluck('name', 'id'))

->afterStateUpdated(function (?string $state, ?Model $record) {
$record->sms_type_id = $state;
// $record->location_id = $location->id; <---need to set this parent ID relationship.
$record->save();
}),
I need to get a reference to the location for this particular page, which is the parent record of the above. I've tried using Get $get, I've tried referencing the Request since the ID I need is in the URL, but because it's just a livewire update call, the request doesn't help me.
Solution:
Or maybe something like ```php Tab::make('config') ->label('Config')...
Jump to solution
4 Replies
bernhard
bernhard11mo ago
Is it in the panel or outside? Depending on where u are, you can try $form->getRecord()
Solution
bernhard
bernhard11mo ago
Or maybe something like
Tab::make('config')
->label('Config')
->schema(function($record) {
$parent = $record;
return [
FieldSet::make()
->relationship('detail')
->schema([
Select::make('sms_type_id')
->options(SmsType::all()->pluck('name', 'id'))
->afterStateUpdated(function (?string $state, ?Model $record) use ($parent) {
$record->sms_type_id = $state;
$record->location_id = $parent->id; <---need to set this parent ID relationship.
$record->save();
}),
])
];
})
Tab::make('config')
->label('Config')
->schema(function($record) {
$parent = $record;
return [
FieldSet::make()
->relationship('detail')
->schema([
Select::make('sms_type_id')
->options(SmsType::all()->pluck('name', 'id'))
->afterStateUpdated(function (?string $state, ?Model $record) use ($parent) {
$record->sms_type_id = $state;
$record->location_id = $parent->id; <---need to set this parent ID relationship.
$record->save();
}),
])
];
})
bernhard
bernhard11mo ago
When inside a regular form, you should have access to $this->data or something
Jon Mason
Jon MasonOP11mo ago
that got me working, thanks!
Want results from more Discord servers?
Add your server