https://github.com/rootinc/laravel-db-url
Package to translate a database URI into database config values.
https://github.com/rootinc/laravel-db-url
Last synced: about 1 year ago
JSON representation
Package to translate a database URI into database config values.
- Host: GitHub
- URL: https://github.com/rootinc/laravel-db-url
- Owner: rootinc
- Created: 2019-03-26T21:13:21.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-04-30T18:58:23.000Z (about 6 years ago)
- Last Synced: 2025-03-25T21:11:13.345Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Database URL
Drop-in package to translate a database URL into database config values. No more need to add logic to your
`config/database.php` file.
This is meant to help prevent the need for manually parsing database URLs, like the ones that come from Heroku or AWS.
It will take a URL and assign host, password, username, port, (etc.) components to the default driver or
specific driver(s) of your choosing.
This __will override__, at runtime, any `default` database connection values for `host`, `port`, `username`, `password`, and `database`
> Laravel 5.8+ supports Database URL config officially. See [Laravel Database Config](https://laravel.com/docs/5.8/database#configuration).
## Requirements
- PHP >= 7.1.3
- Laravel 5.6 or 5.7
## Installation
`$ composer require rootinc/laravel-db-url`
## Configuration
The package will automatically map the parsed values from the environment variable `DATABASE_URL` to the `default` database connection.
Add a `DATABASE_URL` entry to your `.env` or server environment with a URL to your default database.
`DATABASE_URL=postgresql://username:password@localhost:5432/database-name`
This will overwrite database connection values for `host`, `port`, `username`, `password`, and `database`
## Customization
Override default behaviour by publishing the config file and setting values.
`$ php artisan vendor:publish --tag=db-url`
Set any database connections by supplying `default` or the key path, like `connections.pgsql`, or `redis`
to have a URL mapped onto the connection at that key path.
config/db-url.php
```
return [
'default' => 'SOME_DATABASE_URL', // "default" resolves key path from default key
'connections.pgsql' => 'OTHER_PGSQL_URL', // Set the "pgsql" driver with different URL. Same when "default" set to "pgsql"
'connections.mysql' => 'OTHER_MYSQL_URL', // Set the "mysql" driver with different URL
];
```
.env
```
DATABASE_URL=postgresql://username:password@localhost:5432/database-name
OTHER_MYSQL_URL=mysql://username:password@localhost:3306/db_example
```