3
votes

Laravel database configuration requires setting the host, database, username and password for a mysql or postgres database in config/database.php. However when adding a database in Heroku, it is specified as a single URL usually set as an environment variable DATABASE_URL. I looked in the Laravel database connectors but found no solution to use the URL, so I set each host, database, etc. Is there a way to use the URL directly?

1
Did you figure this out?Alias
Looking for the same thing, I was thinking of maybe using a regexp to extract the information from the DATABASE_URL, but if there is a url connector, better.Hernan Rajchert

1 Answers

4
votes
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));

$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);

More background info about using mysql/cleardb on heroku: http://mattstauffer.co/blog/laravel-on-heroku-using-a-mysql-database