Updating a date with a Table Toggle.

Hi, I have a dateTime filed called archived_on. I'd like to edit this column using a toggle field in a table. Specifically, when the toggle is switched to "off", I want the date field to be set to
null
, when the toggle is switched to "on", I want the field to be set to now(). How can I go about this?
Solution
ToggleColumn::make('archived_on')
    ->label('Archived')
    ->updateStateUsing(function ($state, $record) {
        $record->archived_on = $state
            ? now()
            : null;
        $record->save();

        return $state;
    }),
Was this page helpful?