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

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

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

出现以下错误:

Fatal error: Call to a member function connection() on a non-object in /path/to/the/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 3132

必须懒惰地加载依赖于数据库的配置文件.只需将它们放在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));
    }
}
https://stackoverflow.com/questions/29203532/eloquent-query-in-custom-config-file-config-laravel-5

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:自定义配置文件(/ config)Laravel 5中的雄辩查询