Search code examples
laravellaravel-filamentfilamentphp

Is it possible to make a Filament (version 2) Action reactive?


Can I get a Filament (v2) Action to refresh the whole page, or at least update a Navigation Badge?

I've got a table of Recipes, and I've got a Single Action on that recipe table, which adds the recipe to a Menu.

And i've got a Resource for the Menu, which has a Navigation Badge which shows the number of Recipes in that Menu.

But when I click the Action to add a Recipe to the Menu, it doesn't immediately update the Navigation Badge. I have to refresh the page in the browser to see the updated Navigation Badge.

Is there some way I can get the Action to be Reactive, so that it updates the whole page? Or even a way to get the Action to trigger a refresh on the Nav Badge?


Solution

  • You can add redirect to action

        class CreateProduct extends CreateRecord
    {
        protected static string $resource = ProductResource::class;
    
        protected function getRedirectUrl(): string
        {
            return $this->getResource()::getUrl('index');
        }
    }
    

    or you can use livewire (lifecycle hooks )

    https://filamentphp.com/docs/2.x/admin/resources/editing-records#lifecycle-hooks

    it will look like this

     ->actions([
                    Tables\Actions\EditAction::make()
                        ->after(function (Component $livewire) { 
                            $livewire->dispatch('refreshProducts');
                        }),
    

    we need to listen for the event and refresh the page. (in editFileName.php )

    use Livewire\Attributes\On;
     
    
        class EditProduct extends EditRecord
        {
            // ...
         
            #[On('refreshProducts')]
            public function refresh(): void
            {
            }
        }