F
Filament14mo ago
Hemith

Refetch RelationManager data on parent resource save

Is there a way to refetch the query data for a relationmanager when the parent resource data is saved? Currently I just reload the page after I finished saving. I was wondering if there are any alternatives.
10 Replies
Hemith
HemithOP14mo ago
I got it. I just dispatch an event to the relationmanager from parent resource. This reloads the relationmanager table. Code on Parent Resource:
$this->dispatch('update-balance');
$this->dispatch('update-balance');
Code on RelationManager:
#[On('update-balance')]
public function updateBalance()
{
return;
}
#[On('update-balance')]
public function updateBalance()
{
return;
}
gon.exe
gon.exe13mo ago
Hi @Hemith, I found your message because I need do the same. Could you give more details to achieve that? Where do you added the dispatch function? On the afterSave function from the Edit Page ?
Hemith
HemithOP13mo ago
Hi @gon.exe . Yes, I added it at the EditPage in the beforeSave() function.
gon.exe
gon.exe13mo ago
Hi, thank you so much for your replay I added it on afterSave() function from the EditPage
protected function afterSave(): void {

$this->dispatch('refresh');

}
protected function afterSave(): void {

$this->dispatch('refresh');

}
and then I added a refresh function on my Relation Manager Page:
public function refresh(): void {

dd('hola');

return;
}
public function refresh(): void {

dd('hola');

return;
}
but I get: "Method App\Filament\Resources\AssetResource\Pages\EditAsset::dispatch does not exist."
Hemith
HemithOP13mo ago
hmm that's odd. You are on filament v3, correct? also, could you try running it inside the beforeSave() function?
gon.exe
gon.exe13mo ago
I am running v2. I already tested the beforeSave function
Hemith
HemithOP13mo ago
ohh maybe that's the case. I'm running filamentv3 with livewire3.
gon.exe
gon.exe13mo ago
Thanks, I'll figure out a little more! I got it ! Thanks so much for your help! @Hemith
class EditAsset extends EditRecord
{

...

// Runs after the form fields are saved to the database
protected function afterSave(): void {

// Execute refresh function
$this->emit('refreshRelationManagerTable');
//
}
//

}

class InventoryRelationManager extends RelationManager
{

...

// Refresh table
protected $listeners = ['refreshRelationManagerTable'=>'refreshTable'];
public function refreshTable() : void {
return;
}
//

}
class EditAsset extends EditRecord
{

...

// Runs after the form fields are saved to the database
protected function afterSave(): void {

// Execute refresh function
$this->emit('refreshRelationManagerTable');
//
}
//

}

class InventoryRelationManager extends RelationManager
{

...

// Refresh table
protected $listeners = ['refreshRelationManagerTable'=>'refreshTable'];
public function refreshTable() : void {
return;
}
//

}
Soundmit
Soundmit12mo ago
ciao, i'm trying to update a relationmanager after an infolist action my action open a modal with a select, i can change the value of this select and correctly save in the db but if i want to view the new relation, i need to refresh the page. i'm using filament v3 in my edit resource i have addedd

protected function afterUpdate(): void
{
$this->dispatch('refreshRelation');
}

protected function afterUpdate(): void
{
$this->dispatch('refreshRelation');
}
in my relation manager

...
class RepairLogsRelationManager extends RelationManager
{
protected static string $relationship = 'RepairLogs';
// protected $listeners = ['refreshRelation' => 'refreshTable'];

public function form(Form $form): Form
{
return $form
->schema([

]);
}

#[On('refreshRelation')]
public function refreshRelation()
{
dd('test');
return;
}
...

...
class RepairLogsRelationManager extends RelationManager
{
protected static string $relationship = 'RepairLogs';
// protected $listeners = ['refreshRelation' => 'refreshTable'];

public function form(Form $form): Form
{
return $form
->schema([

]);
}

#[On('refreshRelation')]
public function refreshRelation()
{
dd('test');
return;
}
...
i've put a dd('test') but it never fired solved
Soundmit
Soundmit12mo ago
GitHub
Reload Relation Manager after an Action · filamentphp filament · Di...
How can I make a relation manager reload after an action has completed on an edit or view page?
Want results from more Discord servers?
Add your server