Laravel 5.6执行php artisan config:clear后出现Call to a member function connection() on null错误,命令与项目均无法运行求助
php artisan config:clear in Laravel 5.6 Hey there, let's work through this frustrating issue you're facing. That error typically pops up when your app tries to access the database connection before Laravel has finished loading its configuration—especially after clearing the config cache, which removes the pre-compiled config file Laravel relies on for quick bootstrapping. Here are the actionable steps to fix it:
1. Verify Your .env File Configuration
First, double-check your .env file for missing or incorrect database credentials. Make sure these keys are set correctly (no typos, empty values, or extra spaces):
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_db_user DB_PASSWORD=your_db_password
Also, confirm the .env file is in your project root and has proper read permissions for both your web server and command-line user.
2. Regenerate the Config Cache
Since config:clear deletes the cached config file (bootstrap/cache/config.php), we need to rebuild it to restore proper configuration loading. Run this command first:
php artisan config:cache
If you're targeting a specific environment (like local or production), specify it explicitly:
php artisan config:cache --env=local
This compiles all your config files into a single cached file, helping Laravel load configuration smoothly on boot.
3. Fix Premature Database Calls in Service Providers
A common culprit is custom service providers that run database operations in their boot() method before the app is fully initialized. For example:
// ❌ Bad: Runs a DB query immediately in boot() public function boot() { $siteSettings = SiteSetting::all(); config(['app.site_settings' => $siteSettings]); }
Instead, wrap your database logic in the booted() callback, which triggers only after the entire app has finished booting:
// ✅ Good: Waits for full app initialization public function boot() { $this->app->booted(function () { $siteSettings = SiteSetting::all(); config(['app.site_settings' => $siteSettings]); }); }
If possible, cache these settings to avoid database queries on every boot entirely.
4. Clear All Caches
Stale cached data from other parts of Laravel can interfere with fresh configuration. Clear all caches with these commands:
php artisan cache:clear php artisan route:clear php artisan view:clear
This ensures no leftover cached data conflicts with your updated setup.
5. Fix Directory Permissions
If the above steps fail, check permissions on the bootstrap/cache directory. Laravel needs write access here to generate cache files. Run these commands (adjust user/group for your server):
chmod -R 755 bootstrap/cache chown -R www-data:www-data bootstrap/cache
Replace www-data with your web server's user (e.g., apache on CentOS, nginx on some systems).
6. Manually Restore Cached Config (Last Resort)
If you can't run php artisan commands at all, copy a working config.php file from a backup into bootstrap/cache to get the app running temporarily. Use this time to diagnose and fix the root cause (like incorrect service provider logic) instead of relying on this band-aid.
内容的提问来源于stack exchange,提问作者Devendra Rajput

