outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
273 stars 97 forks source link

Field value should default to null not empty string when resolving in controller #83

Closed JoshMoreno closed 2 years ago

JoshMoreno commented 2 years ago

https://github.com/optimistdigital/nova-settings/blob/master/src/Http/Controllers/SettingsController.php#L36

The value column is nullable and the \OptimistDigital\NovaSettings\Models\Settings model has an accessor defined for value which will return null, but in the controller it's defaulting to an empty string. For what it's worth, nova uses data_get($resource, str_replace('->', '.', $attribute)) which will default to null.

The problem with an empty string is nova is expecting null in their DateTime field and throwing an exception when it's passed an empty string. Here's their code for reference:

if (! is_null($value)) {
    if ($value instanceof DateTimeInterface) {
        return $value->format($this->dateFormat);
    }

    throw new Exception("DateTime field must cast to 'datetime' in Eloquent model.");
}

I think this is only an issue for fields that have nested fields (aka $field->meta['fields']) since findOrNew is used for regular fields. I'll submit a PR in a minute