Nullable dates in spatie/laravel-settings

Can anyone provide a basic example of how to define a settings class with a nullable date property?

I have this:

class MySettings extends Settings
{
    public ?DateTimeInterface $dateFrom;
    public ?DateTimeInterface $dateFrom;

    public static function group(): string
    {
        return 'dates';
    }
}


and a Page which provides a form with DateTimePicker::make('dateFrom')->seconds(false)->native(false); but every time I try to save where there is a date set, I am getting an error saying:

Cannot assign string to property App\Settings\MySettings::$dateFrom of type ?DateTimeInterface

From reading the docs, I can't tell what else would be required? The global cast for DateTimeInterface is set up per the docs and default config publication.
Solution
I think that the problem is that you are saying the property has to be a Carbon/DateTime instance but your trying to set it as a string. Easyist thing to do would probably be to convert the string to a carbon instance.

DateTimePicker::make('showFrom')
    ->label('Show From')
    ->prefixIcon('heroicon-o-calendar')
    ->seconds(false)
    ->native(false)
    ->format('Y-m-d H:i:s')
    ->dehydrateStateUsing(fn ($state) => Carbon::parse($state)),
Was this page helpful?