Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ringo's avatar
Level 1

Laravel env helper function and database.php

I uploaded my app on heroku, and to use heroku "cleardb mysql" addon, I fixed part of my database.php file like below.

'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', parse_url(getenv("CLEARDB_DATABASE_URL"))["host"]), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', substr(parse_url(getenv("CLEARDB_DATABASE_URL"))["path"], 1)), 'username' => env('DB_USERNAME', parse_url(getenv("CLEARDB_DATABASE_URL"))["user"]), 'password' => env('DB_PASSWORD', parse_url(getenv("CLEARDB_DATABASE_URL"))["pass"]), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], I thought, env function would retrieve my local database in local dev environment, because it has .evn file in my local project folder, and on heroku, i thought it would retrieve heroku database, because i did not push .env file to heroku server.

However, in local environment, i get these error messages

Notice: Undefined index: host in /home/daewoong/workspace/LaravelEx/config/database.php on line 43 Notice: Undefined index: user in /home/daewoong/workspace/LaravelEx/config/database.php on line 46 Notice: Undefined index: pass in /home/daewoong/workspace/LaravelEx/config/database.php on line 47

and neither it works as i expected on heroku.

0 likes
2 replies
davy_yg's avatar

Cek your database configuration:

config/database.php

ringo's avatar
Level 1

@davy_yg above code is part of my database.php file. And there is nothing wrong with syntax.

Please or to participate in this conversation.