自定义配置文件(/ config)中的雄辩查询Laravel 5 [英] Eloquent query in custom config file (/config) Laravel 5
问题描述
尝试在自定义配置文件中启动以下Eloquent查询,该配置文件位于:Laravel 5中的 / config
目录:
Trying to fire the following Eloquent query in a custom config file which is present in: /config
directory in Laravel 5:
'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()
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
The config files that depend on database must be loaded lazily. Just put them inside a new config.lazy
folder next to config
folder and add the following method inside your 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));
}
}
这篇关于自定义配置文件(/ config)中的雄辩查询Laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!