jldavis76
jldavis76

Reputation: 822

Database configuration in Laravel 5.2

I recently began a new project with a fresh install of Laravel 5.2, and I am a little confused with a few things involved in configuring the database. I want to use sqlite as my database, and I was under the impression all I had to do was change the config/database file so that the 'default' value was set to sqlite, and then create a database.sqlite file to be used for that database.

So, I changed my config/database file to look like the following:

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,
    ],

],

];

This, however, did not work. It turns out, all I had to do was change the .env file so that it DB_CONNECTION was equal to sqlite. So that file currently is set to:

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

Now, the application works fine.

So, I guess my question is, what is the point of changing anything in the config/database file if it doesn't change the env variables? Am I missing something?

Thanks!

Upvotes: 0

Views: 2790

Answers (2)

Jonio
Jonio

Reputation: 1293

Try to change de env file:

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

Upvotes: 0

Jilson Thomas
Jilson Thomas

Reputation: 7313

The idea is to change the .env file to make the changes reflect in the database.php file.

If you are collaborating with someone for a project, and if you want to share the code with them, the database.php file should also be shared. So this will contain all your credentials for the database which you don't want to make it public.

So inorder to prevent that, you create a .env variable and reference it in the database.php file. So when you push your code to github or any repository, you can set a rule to ignore the .env file. So this file won't be under the version control and won't get shared among all other collaborators.

So when another project member clone the code from the repository, all they needs to do is to create an .env file and set their own credentials. In this way, all the database connections will be automatically referenced in the database.php file.

Upvotes: 4

Related Questions