Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config Loading Issue: ‘connection on null’ Error in Laravel #49346

Closed
sajadsdi opened this issue Dec 12, 2023 · 1 comment
Closed

Config Loading Issue: ‘connection on null’ Error in Laravel #49346

sajadsdi opened this issue Dec 12, 2023 · 1 comment

Comments

@sajadsdi
Copy link

sajadsdi commented Dec 12, 2023

Laravel Version

10.37.1

PHP Version

8.1.13

Database Driver & Version

MySQL 5.7

Description

This error occurs because all configs are loaded at the start of Laravel’s boot process, which creates a limitation for using the configs in Laravel. I have resolved this issue in a pull request at the link below, but I think I did not communicate my point well and it got rejected.

pull requests : #49313

Please advise if you have a better solution to this problem!

Please don’t close this issue quickly; let’s discuss it further as it is very important to me.

Steps To Reproduce

Installation of the laravel-setting-pro package.

I wrote this package myself and am completely confident in it!
GitHub package link: https://github.com/sajadsdi/laravel-setting-pro

Before installation, we deactivate optimization!

php artisan optimize:clear
  • Now we install the package!
composer require sajadsdi/laravel-setting-pro
php artisan setting:publish
php artisan setting:install
  • After installation, we test the package by executing these commands in a test controller:
<?php

namespace App\Http\Controllers;

class TestController extends Controller
{
    public function test()
    {
        dd(setting('test','welcome'), setting('test','version'));
    }
}
php artisan serve
  • We bring up the test route in the browser, and the result we receive is:
"welcome to Laravel Setting Pro" // app\Http\Controllers\TestController.php:10
"1.0.0" // app\Http\Controllers\TestController.php:10
  • So far everything goes very well, then we close the server!

Ctrl + C on terminal

  • We boot it up again.
php artisan serve
  • We see everything is normal! We close the server again.

Ctrl + C on terminal

  • Now we create a config file named custom.php and save it as follows:
<?php

return [
    "massage" => setting('welcome'),
    "version" => setting('test', 'version')
];
  • We serve Laravel.
php artisan serve

And we encounter this error!

In Model.php line 1819:

Call to a member function connection() on null

And here is the laravel.log file

[2023-12-12 20:12:41] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (InvalidArgumentException(code: 0): Log [] is not defined. at D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Log\\LogManager.php:212)
[stacktrace]
#0 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Log\\LogManager.php(137): Illuminate\\Log\\LogManager->resolve(NULL, NULL)
#1 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Log\\LogManager.php(124): Illuminate\\Log\\LogManager->get(NULL)
#2 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Log\\LogManager.php(665): Illuminate\\Log\\LogManager->driver()
#3 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php(317): Illuminate\\Log\\LogManager->error('Call to a membe...', Array)
#4 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php(278): Illuminate\\Foundation\\Exceptions\\Handler->reportThrowable(Object(Error))
#5 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Console\\Kernel.php(523): Illuminate\\Foundation\\Exceptions\\Handler->report(Object(Error))
#6 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Console\\Kernel.php(203): Illuminate\\Foundation\\Console\\Kernel->reportException(Object(Error))
#7 D:\\project\\test-project\\artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#8 {main}
"}
[2023-12-12 20:12:41] laravel.ERROR: Call to a member function connection() on null {"exception":"[object] (Error(code: 0): Call to a member function connection() on null at D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php:1819)
[stacktrace]
#0 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(1785): Illuminate\\Database\\Eloquent\\Model::resolveConnection('mysql')
#1 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(1576): Illuminate\\Database\\Eloquent\\Model->getConnection()
#2 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(1495): Illuminate\\Database\\Eloquent\\Model->newBaseQueryBuilder()
#3 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(1531): Illuminate\\Database\\Eloquent\\Model->newModelQuery()
#4 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(1484): Illuminate\\Database\\Eloquent\\Model->newQueryWithoutScopes()
#5 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(2334): Illuminate\\Database\\Eloquent\\Model->newQuery()
#6 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\Model\\MysqlSetting.php(22): Illuminate\\Database\\Eloquent\\Model->__call('where', Array)
#7 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\Drivers\\Database.php(26): Sajadsdi\\LaravelSettingPro\\Model\\MysqlSetting->getSetting('test')
#8 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\Services\\SettingStore.php(55): Sajadsdi\\LaravelSettingPro\\Drivers\\Database->get('test')
#9 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\Services\\SettingStore.php(39): Sajadsdi\\LaravelSettingPro\\Services\\SettingStore->getSetting('test')
#10 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\LaravelSettingPro.php(154): Sajadsdi\\LaravelSettingPro\\Services\\SettingStore->get('test')
#11 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\LaravelSettingPro.php(56): Sajadsdi\\LaravelSettingPro\\LaravelSettingPro->load('test', 'get')
#12 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\Support\\Setting.php(86): Sajadsdi\\LaravelSettingPro\\LaravelSettingPro->get('test', 'version', NULL, true)
#13 D:\\project\\test-project\\vendor\\sajadsdi\\laravel-setting-pro\\src\\Support\\helpers.php(25): Sajadsdi\\LaravelSettingPro\\Support\\Setting->get('version', NULL)
#14 D:\\project\\test-project\\config\\custom.php(4): setting('test', 'version')
#15 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\LoadConfiguration.php(70): require('D:\\\\project\\\\test...')
#16 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\LoadConfiguration.php(39): Illuminate\\Foundation\\Bootstrap\\LoadConfiguration->loadConfigurationFiles(Object(Illuminate\\Foundation\\Application), Object(Illuminate\\Config\\Repository))
#17 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(261): Illuminate\\Foundation\\Bootstrap\\LoadConfiguration->bootstrap(Object(Illuminate\\Foundation\\Application))
#18 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Console\\Kernel.php(447): Illuminate\\Foundation\\Application->bootstrapWith(Array)
#19 D:\\project\\test-project\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Console\\Kernel.php(199): Illuminate\\Foundation\\Console\\Kernel->bootstrap()
#20 D:\\project\\test-project\\artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#21 {main}
"}

Full error

Error:
Call to a member function connection() on null

  at D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1819
  at Illuminate\Database\Eloquent\Model::resolveConnection('mysql')
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1785)
  at Illuminate\Database\Eloquent\Model->getConnection()
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1576)
  at Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1495)
  at Illuminate\Database\Eloquent\Model->newModelQuery()
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1531)
  at Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1484)
  at Illuminate\Database\Eloquent\Model->newQuery()
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:2334)
  at Illuminate\Database\Eloquent\Model->__call('where', array('setting', 'test'))
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\Model\MysqlSetting.php:22)
  at Sajadsdi\LaravelSettingPro\Model\MysqlSetting->getSetting('test')
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\Drivers\Database.php:26)
  at Sajadsdi\LaravelSettingPro\Drivers\Database->get('test')
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\Services\SettingStore.php:55)
  at Sajadsdi\LaravelSettingPro\Services\SettingStore->getSetting('test')
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\Services\SettingStore.php:39)
  at Sajadsdi\LaravelSettingPro\Services\SettingStore->get('test')
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\LaravelSettingPro.php:154)
  at Sajadsdi\LaravelSettingPro\LaravelSettingPro->load('test', 'get')
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\LaravelSettingPro.php:56)
  at Sajadsdi\LaravelSettingPro\LaravelSettingPro->get('test', 'version', null, true)
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\Support\Setting.php:86)
  at Sajadsdi\LaravelSettingPro\Support\Setting->get('version', null)
     (D:\project\test-project\vendor\sajadsdi\laravel-setting-pro\src\Support\helpers.php:25)
  at setting('test', 'version')
     (D:\project\test-project\config\custom.php:4)
  at require('D:\\project\\test-project\\config\\custom.php')
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\LoadConfiguration.php:70)
  at Illuminate\Foundation\Bootstrap\LoadConfiguration->loadConfigurationFiles(object(Application), object(Repository))
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\LoadConfiguration.php:39)
  at Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap(object(Application))
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:261)
  at Illuminate\Foundation\Application->bootstrapWith(array('Illuminate\\Foundation\\Bootstrap\\LoadEnvironmentVariables', 'Illuminate\\Foundation\\Bootstrap\\LoadConfiguration', 'Illuminate\\Foundation\\Bootstrap\\HandleExceptions', 'Illuminate\\Foundation\\Bootstrap\\RegisterFacades', 'Illuminate\\Foundation\\Bootstrap\\RegisterProviders', 'Illuminate\\Foundation\\Bootstrap\\BootProviders'))
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:186)
  at Illuminate\Foundation\Http\Kernel->bootstrap()
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:170)
  at Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(object(Request))
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:144)
  at Illuminate\Foundation\Http\Kernel->handle(object(Request))
     (D:\project\test-project\public\index.php:52)
  at require_once('D:\\project\\test-project\\public\\index.php')
     (D:\project\test-project\vendor\laravel\framework\src\Illuminate\Foundation\resources\server.php:16)
@sajadsdi sajadsdi changed the title Config Loading Order Causes ‘connection on null’ Error in Laravel Config Loading Issue: ‘connection on null’ Error in Laravel Dec 12, 2023
@sajadsdi
Copy link
Author

@taylorotwell @driesvints please let’s discuss it further.

@laravel laravel locked and limited conversation to collaborators Dec 12, 2023
@crynobone crynobone converted this issue into discussion #49348 Dec 12, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant