Laravel Framework: Easily Connect Your Database

Laravel framework makes it very easy or flexible to manage your database connections through app/config/database.php and through .env.

Given, Below Code is how you have to do it using the app/config/database.php file.


When you first go to that file you will able to find this type of code.

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),

'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

Modify the following property in your app/config/database.php file according to your database settings.

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'Your_Database'),<--- your database name here
'username' => env('DB_USERNAME', 'root/username'),<--Enter the username here'password' => env('DB_PASSWORD', 'password'),<--if there is no password make it empty
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],


Let's do it using the .env file. Initially the .env file is .env.sample rename it to .env . Then you can find code like below


DB_HOST=YOUR_HOST
DB_DATABASE=YOUR_DATABASE
DB_USERNAME=YOUR_USERNAME
DB_PASSWORD=YOUR_PASSWORD

Update the following property in your .env file according to your database settings.

DB_HOST=localhost                                          <------- Your host localhost or 127.0.0.1
DB_DATABASE=your_database_name_here <------- your database name here
DB_USERNAME=root/username                    <------Enter the username here
DB_PASSWORD=password                              <----- If there is no password make it empty

Once you have configured correctly you will be able to access your database.

To test if you have successfully connected your Laravel project to the database you can run the migration command. Laravel project by default comes with some default tables for storing users and their password requests. When you run migrate command you it should create the default tables for you in the database.

For that use the command below.

PHP Artisan Migrate

Contact us for Laravel Development Services.

Related Posts

Technical Solutions

Let’s Check Some Important Eye-Opening Website Facts

It’s End of 2019—so you’ve probably realized by now that your website has an effect on the perceived credibility and quality of your business. But very few people know just how colossal this effect actually Read more…

Technical Solutions

Integrate Zoho CRM For Effective Business Management

Customer Relationship Management (CRM) by Zoho is an approach that helps businesses in maintaining a better relationship with existing customers and brings in more customers. Zoho CRM software is an entire strategy to take up Read more…

Technical Solutions

Steps to Improve Performance of Node Servers

  In the past, Node.js Development was often not an option when building applications that require CPU intensive computation. This is due to its non-blocking, event-driven I/O architecture. With the advent of thread workers in Read more…