-2

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

我有一个案例,我在 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);

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

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

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

有这样的事吗?

4

1 回答 1

0

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

if (config()->has('some.key')) {
    // Get configuration value from config file
} else {
    // Get configuration value from database
}
于 2021-10-14T07:06:17.593 回答