自定义配置文件 (/config) Laravel 5 中的 Eloquent 查询 [英] Eloquent query in custom config file (/config) Laravel 5

查看:27
本文介绍了自定义配置文件 (/config) Laravel 5 中的 Eloquent 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在自定义配置文件中触发以下 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' =>(AppMyAppModelsModelName::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 (SymfonyComponentFinderFinder::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 中的 Eloquent 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆