Laravel 5.2中的数据库配置


Database configuration in Laravel 5.2

我最近开始了一个新项目,新安装了Laravel 5.2,我对配置数据库所涉及的一些事情有点困惑。我想使用sqlite作为我的数据库,我觉得我所要做的就是更改配置/数据库文件,使"默认"值设置为sqlite,然后创建一个用于该数据库的database.sqlite文件。

因此,我更改了我的配置/数据库文件,如下所示:

return [
/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/
'fetch' => PDO::FETCH_CLASS,
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'sqlite'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
    'sqlite' => [
        'driver' => 'sqlite',
        'database' => database_path('database.sqlite'),
        'prefix' => '',
    ],
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '8889'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],
    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
    ],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
    'cluster' => false,
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
],
];

然而,这并没有奏效。事实证明,我所要做的就是更改.env文件,使其DB_CONNECTION等于sqlite。因此该文件当前设置为:

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:dUJjtQsUvjyT1zsHYDYVNUAHygIGMWj4Yu7N4CduAzg=
APP_URL=http://localhost
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

现在,应用程序运行良好。

所以,我想我的问题是,如果不更改env变量,那么更改config/database文件中的任何内容有什么意义?我是不是错过了什么?

谢谢!

我们的想法是更改.env文件,使更改反映在database.php文件中。

如果您正在与某人合作一个项目,并且希望与他们共享代码,则还应该共享database.php文件。因此,这将包含您不想公开的数据库的所有凭据。

为了防止这种情况,您创建了一个.env变量,并在database.php文件中引用它。因此,当您将代码推送到github或任何存储库时,您可以设置一个规则来忽略.env文件。因此,该文件将不受版本控制,也不会在所有其他合作者之间共享。

因此,当另一个项目成员从存储库中克隆代码时,他们所需要做的就是创建一个.env文件并设置自己的凭据。这样,所有数据库连接都将在database.php文件中自动引用。

尝试更改环境文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=admin
DB_PASSWORD=adminpass