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!

Fixing "The POST method is not supported for route admin/login" in Laravel 12 with Filament 3.2 on aaPanel

When deploying a Laravel 12 application using Filament 3.2 on an Ubuntu 24 server with aaPanel, you might run into the following error:

The POST method is not supported for route admin/login. Supported methods: GET, HEAD.

✅ The Problem

This error typically doesn't appear when you're working locally. Everything might work perfectly on your local development machine, but as soon as you deploy it to a production server using aaPanel, you face this issue.

The core of the problem lies in how Nginx (via aaPanel) handles URL rewriting. Laravel relies on correctly passing all requests to index.php, especially when handling POST requests like login attempts.

By default, if Nginx is not properly configured, it may only handle GET requests correctly while ignoring POST routes (such as admin/login from Filament).

✅ The Solution

To solve this issue, you need to modify the Nginx configuration for your site via aaPanel. Specifically, update the URL rewrite rules to ensure all request methods (GET, POST, etc.) are correctly forwarded to Laravel's index.php.

Update your Nginx rewrite configuration to the following:

location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}

location ^~ /livewire {
try_files $uri $uri/ /index.php?$query_string;
}

 

✅ How to Apply the Fix in aaPanel

  1. Log in to your aaPanel dashboard.

  2. Go to Website > select your Laravel domain > click Config.

  3. Scroll down to find the URL rewrite section.

  4. Replace the existing rewrite rules with the ones above.

  5. Save the configuration and reload Nginx.

✅ Why This Works

  • The try_files directive ensures that any request (including POST) will be passed to index.php if no file or directory is found that matches the URI.

  • Laravel’s routing system will then properly handle the request, including POST requests to /admin/login.

  • The /livewire block ensures Filament’s Livewire components also work correctly.

✅ Final Notes

  • Always clear your Laravel cache after deployment:

    php artisan config:clear
    php artisan route:clear
    php artisan view:clear
    php artisan cache:clear