Laravel 5.2中的数据库配置

时间:2016-04-06 21:14:22

标签: php laravel configuration environment-variables laravel-5.2

我最近开始了一个全新安装Laravel 5.2的新项目,我对配置数据库涉及的一些事情感到困惑。我想使用sqlite作为我的数据库,我的印象是我所要做的就是更改config / database文件以便将'default'值设置为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文件中的任何内容有什么意义呢?我错过了什么吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

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

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

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

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

答案 1 :(得分:0)

尝试更改环境文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=admin
DB_PASSWORD=adminpass
相关问题