首页 文章

自定义配置文件(/ config)Laravel 5中的雄辩查询

提问于
浏览
3

尝试在Laravel 5中的 /config 目录中出现的自定义配置文件中触发以下Eloquent查询:

'array_name' =>(App\MyApp\Models\ModelName::lists('column_name', 'column_name')),

收到以下错误:

致命错误:在3132行的/path/to/the/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php中调用非对象上的成员函数connection()

1 回答

  • 0

    必须懒惰地加载依赖于数据库的配置文件 . 只需将它们放在 config 文件夹旁边的新 config.lazy 文件夹中,然后在 app/Providers/COnfigServiceProvider.php 中添加以下方法

    public function boot()
    {
        $envConfigPath = config_path() . '/../config.lazy';
        $config = app('config');
        foreach (\Symfony\Component\Finder\Finder::create()->files()->name('*.php')->in($envConfigPath) as $file)
        {
            $key_name = basename($file->getRealPath(), '.php');
            $old_values = $config->get($key_name) ?: [];
            $new_values = require $file->getRealPath();
            $config->set($key_name, array_replace_recursive($old_values, $new_values));
        }
    }
    

相关问题