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),
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),
5 Replies
Darpan
DarpanOP2y ago
Is this possible?
LeandroFerreira
Filament
Actions - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
Darpan
DarpanOP2y ago
It works using single action, so not possible to override ToggleColumn?
LeandroFerreira
I'm not sure, but I would use a single action. You can also do it:
public static function table(Table $table): Table
{
$pinnedLimit = News::wherePinned(true)->count() >= 3;
...

//in your single action
->hidden(function ($record) use ($pinnedLimit) {
return $record->pinned === 0 && $pinnedLimit;
})
public static function table(Table $table): Table
{
$pinnedLimit = News::wherePinned(true)->count() >= 3;
...

//in your single action
->hidden(function ($record) use ($pinnedLimit) {
return $record->pinned === 0 && $pinnedLimit;
})
Darpan
DarpanOP2y ago
Thanks, it works using single action.
Want results from more Discord servers?
Add your server