【问题标题】:Check if Config item exists检查配置项是否存在
【发布时间】:2021-12-02 13:48:15
【问题描述】:

我想知道是否有办法检查配置项是否存在。

我有一个案例,我在 config/custom.php 文件中引用了一些配置项,而在数据库表中引用了其他配置项。

这个概念是利用 config/custom.php 中存在的现有配置项,当它们不存在时,我从我的数据库中提取它们。

$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);

如您所见,这样做不会满足 NULL 的 FALSE 的配置值。

我想在我的第一行做这样的事情来检查文件中的配置是否“存在”...

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

有这种事吗?

【问题讨论】:

    标签: laravel laravel-config


    【解决方案1】:

    经过搜索找到了解决方案。这是可以提供帮助的解决方案

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

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 2020-01-18
      • 1970-01-01
      相关资源
      最近更新 更多