dwenaus
dwenaus

Reputation: 3326

Connecting Laravel to Heroku database via URL

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?

Upvotes: 4

Views: 2988

Answers (1)

dontmentionthebackup
dontmentionthebackup

Reputation: 2915

$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

Upvotes: 6

Related Questions