FilamentF
Filament14mo ago
Arjan

Follow/Unfollow record is not working

I want to toggle follow/unfollow actions for each $record (table row):

Action::make('follow')
                    ->label('Volgen')
                    ->visible(fn(Series $record) => !auth()->user()->series->contains($record))
                    ->color('info')
                    ->icon('heroicon-o-star')
                    ->action(function (Action $action, ListAllSeries $livewire, Series $record, SeriesService $service) {
                        $service->follow($record);
                        $action->success();
                        $livewire->getTableRecords()->fresh();
                        Notification::make()
                            ->title("{$record->name} gevolgd")
                            ->icon('heroicon-o-star')
                            ->iconColor('success')
                            ->send();
                    }),
                Action::make('unfollow')
                    ->label('Volgend')
                    ->visible(fn(Series $record) => auth()->user()->series->contains($record))
                    ->color('success')
                    ->icon('heroicon-o-star')
                    ->action(function (Action $action, Series $record, SeriesService $service) {
                        $service->unfollow($record);
                        Notification::make()
                            ->title("{$record->name} ontvolgd")
                            ->icon('heroicon-o-star')
                            ->iconColor('primary')
                            ->send();
                    }),


However, when I click 'follow,' the action gets executed, but the 'follow' action does not get hidden, and the unfollow action does not get shown.

I am using

$livewire->getTableRecords()->fresh();


but this does not work. Please help.
Solution
Thank you Leandro. I indeed start using the Toggle column:

ToggleColumn::make('users.id')
                    ->label('Volgen')
                    ->onIcon('gmdi-star')
                    ->offIcon('gmdi-star-border')
                    ->onColor('success')
                    ->beforeStateUpdated(function ($record, $state, SeriesService $service) {
                        $state == true ? $service->follow($record) : $service->unfollow($record);
                    })
                    ->afterStateUpdated(function ($record, $state) {
                        // Runs after the state is saved to the database.
                    }),
Was this page helpful?