Check if Config item exists

1.8k Views Asked by At

I would like to know if there is a way to check if a config item exists.

I have a case where I refer to some config items in the config/custom.php file, and others in a database table.

The concept is to make use of existing config items that exist in config/custom.php, and when they don't exist, I pull them from my database.

$config = Config::get($configtype . '.' . $configname);

if (!$config){
    // if config not found, then get it from the database
    $configrecord = Newconfigs::where(['name' => $configname])->get()->first();
    if (!$configrecord){
        $config = false;
    } else{
        $config = $configrecord->value;
    }
}

return ($config);

As you can see, doing it this way will not cater for config values of NULL of FALSE.

I would like to do something like this in my very first line to check if the config "exists" in the file...

If(Config::exists($configtype . '.' . $configname)){ } else{ //get from database }

Is there such a thing?

1

There are 1 best solutions below

0
On

After searching found a solution. Here is the solution that can help

if (config()->has('some.key')) {
    // Get configuration value from config file
} else {
    // Get configuration value from database
}