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

时间:2015-03-23 04:57:48

标签: laravel eloquent laravel-5

尝试在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()< / p>

1 个答案:

答案 0 :(得分:0)

必须懒惰地加载依赖于数据库的配置文件。只需将它们放在config.lazy文件夹旁边的新config文件夹中,然后在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));
    }
}