How to put action on toggleColumn?

Is it possible to do like this? I want to use the toggle to block user the case is the blocked_at = timestamp blocked_by = unsignedBigInt
Tables\Columns\ToggleColumn::make('blocked_at')
->label('Block')
->sortable()
->onColor('success')
->offColor('danger')
->action(function ($record, $state) {

$blocked_at = $state ? now() : null;
$blocked_by = $state ? auth()->id() : null;

$record->update([
'blocked_at' => $blocked_at,
'blocked_by' => $blocked_by,
]);
}),
Tables\Columns\ToggleColumn::make('blocked_at')
->label('Block')
->sortable()
->onColor('success')
->offColor('danger')
->action(function ($record, $state) {

$blocked_at = $state ? now() : null;
$blocked_by = $state ? auth()->id() : null;

$record->update([
'blocked_at' => $blocked_at,
'blocked_by' => $blocked_by,
]);
}),
3 Replies
Bruno Pereira
Bruno Pereira4mo ago
Keep in mind that togglecolumn are meant to be used on boolean columns and not dates. Unless you have a boolean column "is_blocked" and changing this on the toggle you can update the record with the timestamp and user id.
Jerome V
Jerome VOP4mo ago
Yeah, that's what I thinking... but I just created action instead
Tables\Actions\Action::make('block')
->label(fn ($record) => $record->blocked_at ? 'Unblock' : 'Block')
->action(function ($record) {
$record->blocked_at = $record->blocked_at ? null : now();
$record->blocked_by = $record->blocked_at ? auth()->id() : null;
$record->save();
})
->color(fn ($record) => $record->blocked_at ? 'danger' : 'warning')
->requiresConfirmation()
->icon(fn ($record) => $record->blocked_at ? 'heroicon-o-lock-open' : 'heroicon-o-lock-closed'),
Tables\Actions\Action::make('block')
->label(fn ($record) => $record->blocked_at ? 'Unblock' : 'Block')
->action(function ($record) {
$record->blocked_at = $record->blocked_at ? null : now();
$record->blocked_by = $record->blocked_at ? auth()->id() : null;
$record->save();
})
->color(fn ($record) => $record->blocked_at ? 'danger' : 'warning')
->requiresConfirmation()
->icon(fn ($record) => $record->blocked_at ? 'heroicon-o-lock-open' : 'heroicon-o-lock-closed'),
Want results from more Discord servers?
Add your server