Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 956
dwenaus Avatar asked Oct 18 '25 07:10

dwenaus


1 Answers

$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

like image 95
dontmentionthebackup Avatar answered Oct 19 '25 22:10

dontmentionthebackup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!