outl1ne / nova-settings

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

Changing settings in code? #45

Closed TheOddler closed 3 years ago

TheOddler commented 3 years ago

Is there a way to change the settings through code? Something like the helper functions, but to set. For example: nova_set_setting.

I want to use this in our automated tests, and was wondering if there is a better way then to just manually create the models in the database.

TheOddler commented 3 years ago

For now I created a helper class:

class NovaSettingsHelper
{
    public static function setSetting($settingKey, $value)
    {
        $setting = Settings::where('key', $settingKey)->first();

        if (!$setting) {
            $setting = new Settings([
                'key' => $settingKey,
            ]);
        }

        $setting->value = $value;
        $setting->save();
    }
}
Tarpsvo commented 3 years ago

Hey @TheOddler! Sorry for the delay. :)

I added a helper nova_set_setting_value in version 3.1.1.

I decided not to name it nova_set_setting due to the similarity with nova_get_setting. A typo like that could be costly.

Thanks for the suggestion and example! Good luck!

TheOddler commented 3 years ago

Cool, thanks a lot! And fair point, being a bit more verbose can sometimes save a lot of headache.