Nothing to parse https://laravel.com/docs/8.x/database#configuration-using-urls
only setup DATABASE_URL in your .env
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How do I get the database information (like username) when using "DATABASE_URL" instead of "DB_USERNAME" in the .env file?
.env file:
DATABASE_URL=mysql://bob:[email protected]:3306/mydatabase
In a Laravel file somewhere:
dd(config('database.connections.mysql.username')) // Returns "forge", which is not correct.
The current solution I'm using is:
dd(DB::connection()->getConfig()['username']); // Returns "bob"
However, this only works for the current DB connection.
I guess I could parse the DATABASE_URL env variable with a regex for the info. I figure it must be stored somewhere though by Laravel that I'm not seeing.
Much appreciated.
It was right in front of me, lol. I didn't realize I could pass an argument to DB::connection(), and you can pass an argument to getConfig() as well. Laravel makes things so easy.
dd(DB::connection('mysql_connection_name')->getConfig('username'));
Please or to participate in this conversation.