Determining If a Configuration Value Exists

Problem

You want to see if a configuration value exists.

You know you can use a default parameter to the Config::get() method, but you want to be able to determine if the configuration key is even present in your application's config.

Solution

Use Config::has()

if (Config::has('app.mykey'))
{
    echo "Yea! 'mykey' is in config/app.php\n";
}

Discussion

It's interesting how this is implemented.

The Config::has() method actually uses the Config::get() method with a default value of the current microtime(). It's only two lines of code.

$default = microtime(true);
return $this->get($key, $default) !== $default;
comments powered by Disqus