odd form behavior on relationship

From reading through topics and the docs, I've gathered that in order to use a relationship on a form field, I need to use a form layout component and set the relationship. I have a form in a page component that I'm not populating until the user clicks a button. I'm able to populate the form with what I've got right now, but when it comes time to save, $this->form->getState() returns one field but not the one with the relationship. mount method:
$this->form->fill([
'accountNote' => ['notes' => ''],
'preparer_comments' => ''
]);
$this->form->fill([
'accountNote' => ['notes' => ''],
'preparer_comments' => ''
]);
method to populate form:
public function showEditModal($id)
{

// Fetch the row data based on ID
$this->editItem = BalanceSheet::find($id);
// dd($this->editItem);
$this->form->fill([
'preparer_comments' => $this->editItem->preparer_comments,
'accountNote' => $this->editItem->accountNote //can't seem to set just the field on this, have to set the entire relationship.
]);

$this->dispatch('open-modal', id: 'editRowModal');
}
public function showEditModal($id)
{

// Fetch the row data based on ID
$this->editItem = BalanceSheet::find($id);
// dd($this->editItem);
$this->form->fill([
'preparer_comments' => $this->editItem->preparer_comments,
'accountNote' => $this->editItem->accountNote //can't seem to set just the field on this, have to set the entire relationship.
]);

$this->dispatch('open-modal', id: 'editRowModal');
}
the form:
public function form(Form $form): Form
{
return $form->schema([
Grid::make()
->columns(1)
->relationship('accountNote')
->schema([
Textarea::make('notes')
->label('Account Notes')
->rows(6),
]),
Textarea::make('preparer_comments')
->label('Preparer Comments')
->rows(6),

])->statePath('data')
->model(BalanceSheet::class);
}
public function form(Form $form): Form
{
return $form->schema([
Grid::make()
->columns(1)
->relationship('accountNote')
->schema([
Textarea::make('notes')
->label('Account Notes')
->rows(6),
]),
Textarea::make('preparer_comments')
->label('Preparer Comments')
->rows(6),

])->statePath('data')
->model(BalanceSheet::class);
}
3 Replies
toeknee
toeknee3mo ago
That's correct, relationships are saved independently. You can use $this->data if you want the raw data and not the state.
awcodes
awcodes3mo ago
Just keep in mind that $this->data has not been validated.
LeandroFerreira
LeandroFerreira3mo ago
if you want to access it in the $data, add ->dehydrated() after ->relationship()
Want results from more Discord servers?
Add your server