ToggleColumn Action

I have a ToggleColumn on my table that updates the pinned status of news posts. However, I want to limit the number of pinned posts to three, so I have disabled the toggle when the post count is equal to or greater than three.

To ensure that the toggle behaves as intended, I am using the ->action() function. However, it does not seem to be working correctly. I need to investigate this further.

Another issue I have noticed is that when I update the toggle status of a post, the other records in the table do not update (i.e., disable) automatically. Instead, I need to manually refresh the page. I would like to understand why this is happening and find a solution to make the updates happen automatically.

Tables\Columns\ToggleColumn::make('pinned')
                    ->tooltip(fn(News $record): string => $record->pinned ? 'Unpin' : 'Pin')
                    ->action(function (News $record) {

                        // This does not work...

                        if(News::wherePinned(true)->count >= 3) {
                            Notification::make('Error')
                                ->body('You can pin only 3 news.')
                                ->danger()
                                ->send();
                        } else {
                            $record->update(['pinned' => true]);
                        }
                    })
                    ->disabled(fn(News $record) => $record->pinned == false && News::wherePinned(true)->count() >= 3),
Was this page helpful?