F
Filament5mo ago
Ron

reload a Livewire form component after updating the record

I am attempting to refresh a livewire form component after updating the record associated with the form section. I have a form section with a simple schema that just loads a livewire component. That livewire component uses a mount method to retrieve the record and passes this to an infolist. This all works great on initial load, but I have a section footer action that updates the record and I want the infolist to refresh after update.
Section::make('Applicant Recommendation)
->schema([
Livewire::make(ViewApplicantRecommendation::class)
->key('view-applicant-recommendation'),
])
->footerActions([
OverrideRecommendation::action(),
])->footerActionsAlignment(Alignment::End);
Section::make('Applicant Recommendation)
->schema([
Livewire::make(ViewApplicantRecommendation::class)
->key('view-applicant-recommendation'),
])
->footerActions([
OverrideRecommendation::action(),
])->footerActionsAlignment(Alignment::End);
The OverrideRecommendation action has a modal form and then updates the record in the action
->action(function (array $data, Order $record, Component $livewire) {
$record->recommendation->update([
'recommendation' => $data['override_recommendation'],
]);
->action(function (array $data, Order $record, Component $livewire) {
$record->recommendation->update([
'recommendation' => $data['override_recommendation'],
]);
ViewApplicationRecommendation uses an infolist and includes a mount method to get the record
public ?Order $record = null;
public function mount(?Order $record = null): void
{
$this->record = $record;
}
public function recommendationInfolist(Infolist $infolist): Infolist
{

return $infolist
->record($this->record)
->schema([
$this->getReasonsRepeatableEntry(),
]);
}
public ?Order $record = null;
public function mount(?Order $record = null): void
{
$this->record = $record;
}
public function recommendationInfolist(Infolist $infolist): Infolist
{

return $infolist
->record($this->record)
->schema([
$this->getReasonsRepeatableEntry(),
]);
}
In the OverrideRecommendation action I can redirect, which would be fine but it doesn't seem as Livewire assorts redirects to url fragments to position the page to the section.
4 Replies
LeandroFerreira
LeandroFerreira5mo ago
Laravel
Events | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Ron
RonOP5mo ago
It seems as though I need to dispatch an event to my livewire component, and i can find the livewire form component via
->action(function (array $data, Order $record, Component $livewire) {
$component = $livewire->form->getComponent('view-applicant-recommendation');

}
->action(function (array $data, Order $record, Component $livewire) {
$component = $livewire->form->getComponent('view-applicant-recommendation');

}
where this is the form schema
->schema([
Livewire::make(ViewApplicantRecommendation::class)
->key('view-applicant-recommendation'),
])
->schema([
Livewire::make(ViewApplicantRecommendation::class)
->key('view-applicant-recommendation'),
])
But once I have the Livewire form component, I can't see how to get to the actual ViewApplicantRecommendation component. I looked at the code for Filament\Forms\Components\Livewire but didn't see anything obvious on how to get an instance of the Livewire component passed in the make() method
LeandroFerreira
LeandroFerreira5mo ago
shouldn't you inject $livewire and use $livewire->dispatch('xxx') ?
Ron
RonOP5mo ago
I will try that Thank you for your help! That worked.
...
->action(function (array $data, Order $record, Component $livewire) {
$record->recommendation->update([
'recommendation' => $data['override_recommendation'],
]);
$livewire->dispatch('recommendation-updated'):
}
...
->action(function (array $data, Order $record, Component $livewire) {
$record->recommendation->update([
'recommendation' => $data['override_recommendation'],
]);
$livewire->dispatch('recommendation-updated'):
}
In my Livewire component class used in the Livewire::make() call in my form,
#[On('recommendation-updated')]
public function updateRecommendation(): void
{
$this->record = $this->record->refresh()->load('recommendation.reasons');
}

public function recommendationInfolist(Infolist $infolist): Infolist
{

return $infolist
->record($this->record)
->schema([
$this->getReasonsRepeatableEntry(),
]);
}
#[On('recommendation-updated')]
public function updateRecommendation(): void
{
$this->record = $this->record->refresh()->load('recommendation.reasons');
}

public function recommendationInfolist(Infolist $infolist): Infolist
{

return $infolist
->record($this->record)
->schema([
$this->getReasonsRepeatableEntry(),
]);
}
dispatching the update updates the record for the infolist and the infolist is rendered with the changes from the modal form on the action. Thanks again. I spent way too much time on this! I appreciate it very much.
Want results from more Discord servers?
Add your server