Updating fields in a Section when a Select is changed based on a relationship.

For context, this is part of a laboratory system. A doctor completes a requisition, which is captured into the system. The requisition belongs to a patient:
class Requisition extends Model implements Auditable
{
public function patient(): BelongsTo
{
return $this->belongsTo(Patient::class);
}
}
class Requisition extends Model implements Auditable
{
public function patient(): BelongsTo
{
return $this->belongsTo(Patient::class);
}
}
A patient has many requisitions:
class Patient extends Model implements Auditable
{
public function requisition(): HasMany
{
return $this->hasMany(Requisition::class);
}
}
class Patient extends Model implements Auditable
{
public function requisition(): HasMany
{
return $this->hasMany(Requisition::class);
}
}
I need the requisition creation form to be able to select a patient, or create a new one if it exists. This works:
Select::make('patient_id')
->label('Patient')
->relationship('patient', 'full_name')
->searchable(['full_name', 'id_number', 'pat_num'])
->createOptionForm(PatientResource::modal_form()) // Complete form for the modal
->createOptionModalHeading('Create Patient')
->suffixAction(
Action::make('copy_to_guarantor')
->icon('heroicon-m-clipboard')
->action(function (Set $set, $state) {
$set('guarantor_id', $state);
})
)
->required()
->live(),
Select::make('patient_id')
->label('Patient')
->relationship('patient', 'full_name')
->searchable(['full_name', 'id_number', 'pat_num'])
->createOptionForm(PatientResource::modal_form()) // Complete form for the modal
->createOptionModalHeading('Create Patient')
->suffixAction(
Action::make('copy_to_guarantor')
->icon('heroicon-m-clipboard')
->action(function (Set $set, $state) {
$set('guarantor_id', $state);
})
)
->required()
->live(),
In addition, once the patient is selected, it must be displayed so that it can be updated in case any information has changed. I have made the Select live() in the hopes that the chosen or created patient's data can be presented in thuis section for editing:
Section::make()
->relationship('patient')
->schema(
PatientResource::schema() // Array of patient fields
)
->columns(2),
Section::make()
->relationship('patient')
->schema(
PatientResource::schema() // Array of patient fields
)
->columns(2),
I am obviously not doing the thing properly, because the fields in the section do not respond to changes in the select at all. What do I need to do?
1 Reply
WiseWill
WiseWillOP12mo ago
I have made a little progress. I have added a key to my Section:
Section::make()
->key('patient-details')
->relationship('patient')
->schema(
PatientResource::schema() // Array of patient fields
)
->columns(2),
Section::make()
->key('patient-details')
->relationship('patient')
->schema(
PatientResource::schema() // Array of patient fields
)
->columns(2),
and I have added a callback (taken from the docs) to the Select:
Select::make('patient_id')
->label('Patient')
->relationship('patient', 'full_name')
->searchable(['full_name', 'id_number', 'pat_num'])
->native(false)
->createOptionForm(PatientResource::modal_form()) // Complete form for the modal
->createOptionModalHeading('Create Patient')
->suffixAction(
Action::make('copy_to_guarantor')
->icon('heroicon-m-clipboard')
->action(function (Set $set, $state) {
$set('guarantor_id', $state);
})
)
->required()
->live()
->afterStateUpdated(fn (Select $component) => $component
->getContainer()
->getComponent('patient-details')
->getChildComponentContainer()
->fill()
),
Select::make('patient_id')
->label('Patient')
->relationship('patient', 'full_name')
->searchable(['full_name', 'id_number', 'pat_num'])
->native(false)
->createOptionForm(PatientResource::modal_form()) // Complete form for the modal
->createOptionModalHeading('Create Patient')
->suffixAction(
Action::make('copy_to_guarantor')
->icon('heroicon-m-clipboard')
->action(function (Set $set, $state) {
$set('guarantor_id', $state);
})
)
->required()
->live()
->afterStateUpdated(fn (Select $component) => $component
->getContainer()
->getComponent('patient-details')
->getChildComponentContainer()
->fill()
),
Now the patient details in the Section are emptied if I change the patient while editing the requisition. How do I get the actual Patient model to fill those fields with? Got it!
Select::make('patient_id')
->label('Patient')
->relationship('patient', 'full_name')
->searchable(['full_name', 'id_number', 'pat_num'])
->native(false)
->createOptionForm(PatientResource::modal_form()) // Complete form for the modal
->createOptionModalHeading('Create Patient')
->suffixAction(
Action::make('copy_to_guarantor')
->icon('heroicon-m-clipboard')
->action(function (Set $set, $state) {
$set('guarantor_id', $state);
})
)
->required()
->live()
->afterStateUpdated(fn (Select $component) => $component
->getContainer()
->getComponent('patient-details')
->getChildComponentContainer()
->fill(Patient::find($component->getState('patient_id'))->toArray())
),
Select::make('patient_id')
->label('Patient')
->relationship('patient', 'full_name')
->searchable(['full_name', 'id_number', 'pat_num'])
->native(false)
->createOptionForm(PatientResource::modal_form()) // Complete form for the modal
->createOptionModalHeading('Create Patient')
->suffixAction(
Action::make('copy_to_guarantor')
->icon('heroicon-m-clipboard')
->action(function (Set $set, $state) {
$set('guarantor_id', $state);
})
)
->required()
->live()
->afterStateUpdated(fn (Select $component) => $component
->getContainer()
->getComponent('patient-details')
->getChildComponentContainer()
->fill(Patient::find($component->getState('patient_id'))->toArray())
),
Want results from more Discord servers?
Add your server