F
Filament6mo ago
Svet

Update a form field when a notification is received from Edit page

Hey all, I'm trying to update a field in a Filament form when I receive a Laravel notification. I've setup the listeners and I'm receiving the notification fine, but I'm having trouble understanding how to actually update the field from the Edit page. I have very little experience with Filament and Livewire, but have been using Laravel for many years. Currently, I have the following:
// app/Filament/Resources/VideoResource/Pages/EditVideo.php

protected $listeners = [
'echo:videos,ScriptGenerated' => 'updateScript',
];

public function updateScript(): void
{
$this->form->fill([
'script' => $this->record->script,
]);
}
// app/Filament/Resources/VideoResource/Pages/EditVideo.php

protected $listeners = [
'echo:videos,ScriptGenerated' => 'updateScript',
];

public function updateScript(): void
{
$this->form->fill([
'script' => $this->record->script,
]);
}
This kinda works, but it also overwrites all the other fields in the form, which is not what I want. I'm not exactly sure what to ask here, because I'm not entirely certain if what I'm doing is wrong or not. I see there is a $this->form->getLivewire() method and I can change the properties directly from there, but I'm certain this isn't how it's supposed to be done heh. I read the Livewire and Filament documentation on events and notifications, but this is where I got stuck. Google and chatgpt were of no help.
Any help appreciated!
5 Replies
LeandroFerreira
LeandroFerreira5mo ago
try
$this->refreshFormData([
'script',
]);
$this->refreshFormData([
'script',
]);
or $this->data['script'] = $this->record->script
Svet
SvetOP5mo ago
Yup, they both work, thank you Leonardo! Do they both go through all the events and handlers, etc? Because that was my concern with $this->form->getLivewire()->data['script'] = 'blah', i.e. changing the data directly and not through a method that fires any events if needed.
LeandroFerreira
LeandroFerreira5mo ago
you can use refreshFormData.. no issues I guess
Svet
SvetOP5mo ago
Perfect, thank you! I still feel like I'm missing something. In Livewire, if I just update the property, it reactively changes on the frontend. Since Filament is built on top of Livewire, shouldn't that be a supported functionality? Or do we need to do it this way because of all the hooks and events? I can't even seem to find a way to bind a form field to a model property, i.e. wire:model or something similar. Apologies if my questions sound dumb, I'm trying to better understand how it all works under the hood.
PabloZagni
PabloZagni5mo ago
And how do I refresh a field and the relationshipManager in the viewForm ?
Want results from more Discord servers?
Add your server