The Silent Override: Why Your Laravel 5 Database Config Isn't Being Used
You followed the standard installation procedure for Laravel 5 using Composer, verified the framework version is indeed a development build, and configured app/config/database.php to connect to your local MySQL instance with specific credentials. Yet, when you attempt to register a user via the /auth/register endpoint, the application throws an SQLSTATE[28000] [1045] Access denied error.
The root cause is not in your configuration file at all. It lies in a hidden default defined within the framework's core connection logic. When you inspect the stack trace provided by the error log, specifically the call to MySqlConnector->connect, you will see the database name being set to homestead and the username as homestead. This indicates that Laravel is ignoring your custom configuration in favor of a hardcoded default intended for Vagrant VMs.
This specific behavior occurs because the Laravel 5 installation includes a config/database.php file that contains fallback values designed to mimic a Homestead environment. If you do not explicitly define connection details before these defaults are applied, or if the configuration loading logic prioritizes internal constants over your env-based variables in certain contexts, the framework attempts to connect to a non-existent local user named 'homestead'.
To resolve this, you must ensure that your application initializes the database connections using your intended credentials immediately and exclusively. While Laravel 5 allows for flexible configuration, relying on environment variable defaults like env('DB_HOST', 'localhost') can sometimes be ambiguous if the framework's internal bootstrapping logic attempts to merge configurations or if specific service provider settings override your initial setup.
The most robust solution is to explicitly define the database connection parameters in your .env file and ensure that the configuration cache is cleared before running migrations or accessing routes that interact with the database. In Laravel 5, you should verify that the following variables are present and correctly set in your project root's .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=Logintestfive
DB_USERNAME=root
DB_PASSWORD=manasa
Once these values are secured, you must regenerate the configuration cache to ensure Laravel picks up these environment variables instead of falling back to internal defaults. Execute the following command in your terminal:
php artisan config:clear
php artisan cache:clear
After clearing the caches, restart your PHP development server. When you revisit the registration page, the framework should now correctly instantiate a PDO connection using root and manasa for the Logintestfive database on localhost. This ensures that the hardcoded homestead defaults found in the core files are effectively bypassed by your explicit environment configuration.
If you continue to face issues where the default homestead credentials persist despite correct .env settings, check your app/config/database.php file directly. Ensure that the default key is set to 'mysql' and that the database connection array explicitly references the environment variables without relying solely on fallback values that might conflict with internal constants. You may need to modify the configuration array in database.php to remove any hardcoded defaults that could interfere with your .env injection, ensuring a clean slate for the connection factory.
By forcing the application to strictly adhere to your defined environment variables and clearing any cached configuration that might reference legacy Homestead defaults, you restore full control over the database connection logic. This approach aligns with modern Laravel best practices, where environment-specific configurations are paramount for deployment flexibility and security.