3

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')),

Getting the following error:

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

4
  • What do you see in Model.php in line 3132? My version of Laravel has different version of code. Error itself means that some object in null in there. It can be that Laravel has not properly initialized itself when parsin config files. Commented Mar 23, 2015 at 8:36
  • yes you are absolutely right, but is their any way In laravel 5, to fire an eloquent query in custom config file? because in laravel 4.3, firing a eloquent query in custom config file was quite easy Commented Mar 23, 2015 at 9:05
  • 2
    Config file is not a place for querying the db. Do the job in a service provider for example. Commented Mar 23, 2015 at 10:03
  • @ Jarek Tkaczyk how to use query in provider and call this in config? Commented Oct 3, 2016 at 12:11

1 Answer 1

0

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.