rootinc/laravel-db-url

将数据库URL转换为Laravel数据库驱动值。

v2.0.1 2020-04-30 18:55 UTC

This package is auto-updated.

Last update: 2024-08-29 05:06:40 UTC


README

这是一个直接使用的包,可以将数据库URL转换为数据库配置值。不再需要在您的config/database.php文件中添加逻辑。

此功能旨在帮助防止手动解析数据库URL的需求,例如来自Heroku或AWS的URL。它将提取一个URL并将其主机、密码、用户名、端口(等)组件分配给默认驱动或您选择的特定驱动。

在运行时,将覆盖任何default数据库连接的hostportusernamepassworddatabase值。

Laravel 5.8+官方支持数据库URL配置。请参阅Laravel数据库配置

要求

  • PHP >= 7.1.3
  • Laravel 5.6或5.7

安装

$ composer require rootinc/laravel-db-url

配置

该包将自动将环境变量DATABASE_URL中解析出的值映射到default数据库连接。

在您的.env文件或服务器环境中添加一个指向默认数据库的DATABASE_URL条目。DATABASE_URL=postgresql://username:password@localhost:5432/database-name

这将覆盖数据库连接的hostportusernamepassworddatabase值。

自定义

通过发布配置文件并设置值来覆盖默认行为。

$ php artisan vendor:publish --tag=db-url

通过提供default或键路径,例如connections.pgsqlredis,来设置任何数据库连接,以便在该键路径上映射URL。

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