Database Setup in Laravel

Home » Beginner » Database Setup in Laravel

Here we ‘ll look at database setup for a laravel application. Actually there are 2 places where we can include our database setup details like database connection, database host, database port, database name, database username, database password.

Those 2 locations are :

  • .env file( located at root of the project /.env)
  • database.php (loacated at /config/database.php )

.env

.env is created only in freshly installed application, it does not show up when you deploy it. If we want to deply along with .env file then we have remove .env in .gitignore file. By default the .env is in .gitignore as this .env file contains sensible information.

If we open .env file, it looks like

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret

So we can edit the details with our database settings there.

databse.php

For setting up database using config file database.php, open database.php file located at /config/database.php

‘default’ => env(‘DB_CONNECTION’, ‘mysql’), /* |————————————————————————– | 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’ => env(‘DB_DATABASE’, database_path(‘database.sqlite’)), ‘prefix’ => ”, ], ‘mysql’ => [ ‘driver’ => ‘mysql’, ‘host’ => env(‘DB_HOST’, ‘localhost’), ‘port’ => env(‘DB_PORT’, ‘3306’), ‘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’, ], ],

    ‘default’ => env(‘DB_CONNECTION’, ‘mysql’),     |————————————————————————–     |————————————————————————–     | 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.             ‘database’ => env(‘DB_DATABASE’, database_path(‘database.sqlite’)),             ‘host’ => env(‘DB_HOST’, ‘localhost’),             ‘port’ => env(‘DB_PORT’, ‘3306’),             ‘database’ => env(‘DB_DATABASE’, ‘forge’),             ‘username’ => env(‘DB_USERNAME’, ‘forge’),             ‘password’ => env(‘DB_PASSWORD’, ”),             ‘collation’ => ‘utf8_unicode_ci’,             ‘host’ => env(‘DB_HOST’, ‘localhost’),             ‘port’ => env(‘DB_PORT’, ‘5432’),             ‘database’ => env(‘DB_DATABASE’, ‘forge’),             ‘username’ => env(‘DB_USERNAME’, ‘forge’),             ‘password’ => env(‘DB_PASSWORD’, ”),

We can set the default database connection, and can edit the appropriate settings.

This is how Laravel makes connecting with databases extremely simple.Feel free to check previous posts on Laravel Application Structure and Installing Laravel Framework

 

You might also like