Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings() default value use is inconsistent #19

Open
k8n opened this issue Oct 24, 2024 · 0 comments
Open

settings() default value use is inconsistent #19

k8n opened this issue Oct 24, 2024 · 0 comments

Comments

@k8n
Copy link

k8n commented Oct 24, 2024

If the settings model for the following call does not exist, the default value is returned as expected:

settings('app', 'some_value', 5000) // returns 5000

When the setting model does exist and was not initialized (i.e. settings column is null), the expected result is returned:

settings('app', 'some_value', 5000) // returns 5000

However, if the settings model exists, was initialized (edited and saved in Nova), but the key 'some_value' value was not set / left blank (i.e. 'settings' column contains { some_value: null }, the unexpected value is returned:

settings('app', 'some_value', 5000) // returns null

Given the following settings class declaration, one would assume that the default value would be written to the database if not explicitly provided in Nova, but that does not happen. I work around the issue using fillUsing() for now:

class ApplicationSettings extends AbstractType
{
    public function fields(): array
    {
        return [
            Currency::make('Some Value', 'some_value')
                ->default(5000),
                // ->default(fn () => 5000) // also no effect
                // Workaround
                ->fillUsing(function (NovaRequest $request, $model, $attribute) {
                    // if the key exists in the input with null value (and it does when the field is left blank),
                    // the null value is returned; default is not at play
                    $value = $request->input($attribute, 5000) ?? 5000;
                    $model->forceFill([Str::replace('.', '->', $attribute) => $value]);
                })
        ];
    }
}

I'm not sure how this issue can be handled.

Perhaps settings() helper could return the default value if the requested setting resolves to null?

return data_get(
target: $settings,
key: $key,
default: $default,
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant