Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Support database_url for php artisan db command #37064

Merged

Conversation

mmabdelgawad
Copy link

Problem

When you are using DATABASE_URL in your .env and run php artisan db it doesn't open database CLI and throws an error

.env file

DATABASE_URL=mysql://root:password@mysql:3306/database

The thrown error

Symfony\Component\Process\Exception\ProcessFailedException 

The command "'mysql' '--host=127.0.0.1' '--port=3306' '--user=forge' '--default-character-set=utf8mb4' 'forge'" failed.

Exit Code: 1(General error)

As you can see the php artisan db will look for DB_CONNECTION, DB_HOST, DB_PORT etc. in .env file and if they are not exist it will fallback to the default database configuration defined in config/database.php and it will not parse the DATABASE_URL

Solution

To fix this we need check if the url is set and not empty in the $connection array then parse and return the new $connection configuration

if (! empty($connection['url'])) {
    $connection = (new ConfigurationUrlParser)
        ->parseConfiguration($connection);
}

@GrahamCampbell GrahamCampbell changed the title Support database_url for php artisan db command [8.x] Support database_url for php artisan db command Apr 20, 2021
@taylorotwell taylorotwell merged commit c607db9 into laravel:8.x Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants