outl1ne / nova-settings

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

Use nova_get_setting into config/* files ? #113

Closed scramatte closed 2 years ago

scramatte commented 2 years ago

Hello,

I would like to use nova_get_setting into config/* files for notifications, mail and ldap settings I've got the following error

Call to a member function connection() on null

image

Thank you for the help

Tarpsvo commented 2 years ago

You can't use nova-settings in the config files. Nova Settings values depend on a database and config files are created before the database connection is set up (which is also set up using config files, so it's a loop pretty much).

nathan-io commented 2 years ago

We wanted to use these settings in config, so the approach was to create a helper to safely check for the value, then use it in our service providers to set config values:

Helper:

use Illuminate\Support\Facades\Schema;

    function safe_nova_get_setting($settingName, $fallbackValue) {
        $return = $fallbackValue;
        if(Schema::hasTable('nova_settings')) {
            try {
                $return = nova_get_setting($settingName, $fallbackValue);
            } catch (Throwable $e) {}
        }
        return $return;
    }

In AppServiceProvider@boot:

        config([
            'app.timezone' => safe_nova_get_setting('APP_TIMEZONE', 'UTC'),
            // ...
        ]);

This is working fine so far, but hasn't been thoroughly tested.