Tuesday, May 6, 2025

Laravel 12 + Filament: .env Set to MySQL, But Laravel Still Uses SQLite? Here's the Fix

 

If you're developing a Laravel 12 application using Filament Admin Panel, you might run into a confusing issue:

 

    You've already updated your .env file to use mysql, but Laravel still tries to connect using sqlite.

 

This can be frustrating, especially when you're certain the .env file is correctly configured.

📌 Your .env Might Look Like This:

 

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=mysql

DB_USERNAME=mysql

DB_PASSWORD=password

 

But when you run:

 

php artisan tinker

>>> env('DB_CONNECTION')

You get:

null

Or:

"sqlite"

 

🔍 Root Cause: Laravel Is Using Cached Configuration

 

Laravel uses configuration caching to boost performance. If you’ve previously run:

 

php artisan config:cache

 

Laravel will no longer read the .env file directly, and instead use the cached version stored in:

 

bootstrap/cache/config.php

 

Solution: Remove the Cached Configuration

 

To fix this, simply delete the cached config file and clear Laravel’s caches.

1. Delete the cached config file

 

Run this in the root directory of your Laravel project:

 

rm bootstrap/cache/config.php

 

2. Clear Laravel’s config and cache

 

Then run:

 

php artisan config:clear

php artisan cache:clear

 

3. Test Again

 

Open Tinker:

 

php artisan tinker

>>> env('DB_CONNECTION')

 

You should now see:

 

"mysql"

 

You can also confirm it by running:

 

>>> DB::connection()->getDatabaseName();

 

🧠 Additional Notes

 

    Make sure your .env file is named correctly and located in the root folder of your project.

 

    Also verify your config/database.php is not hardcoded to 'sqlite'.

 

It should use the environment variable:

 

'default' => env('DB_CONNECTION', 'mysql'),

 

🎯 Conclusion

 

If Laravel continues to use sqlite despite your .env being set to mysql, the most likely reason is that it’s still using cached configuration. Just delete bootstrap/cache/config.php and clear the config cache — problem solved.

 

Hope this helps!

No comments:

Post a Comment