Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

How to used toggleColumn to a timestamp data type?

I tried to used toggleColumn to block a user in the table lis, The problem is the blocked_at is timestamp.. I used hook beforeStateUpdated for now and was able to store data in the database but got some problem when I click the toggle.

I got this error

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1' for column 'blocked_at' at row 1 (Connection: mysql, SQL: update `users` set `blocked_at` = 1, `users`.`updated_at` = 2024-08-10 07:30:10 where `id` = 4)

This is my code

 Tables\Columns\ToggleColumn::make('blocked_at')
                    ->label('Block')
                    ->onColor('success')
                    ->offColor('danger')
                    ->getStateUsing(fn ($record) => !is_null($record->blocked_at))
                    ->beforeStateUpdated(function (bool $state, $record) {
                        $record->blocked_at = $state ? now() : null;
                        $record->blocked_by = $state ? auth()->id() : null;
                        $record->save();
                    }),
0 likes
3 replies
anonymouse703's avatar

@Tray2 Toggle Column in filament is dedicated for boolean only... so I just make 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'),

Please or to participate in this conversation.