• +91 9806724185,+91 9685733903
  • +91 9806724185

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

How to send Bulk Email using Laravel Queue feature?

In Laravel Development  Services, Are you want to Send bulk Emails, it's better to use Queues. Most of the developers nowadays using Queues for sending those Bulk emails. Why this? It speeds up web requests Read more…

Technical Solutions

Assist your business with Zoho Integration Services in 2019

If your handling many small businesses at a time then, taking your apps to the cloud, you may have come across Zoho Services. Some say that Zoho is just a Google Apps knockoff, it's not. Read more…

Technical Solutions

Google Maps v/s Bing Maps: Comparing two Giants Corporate

Everyone wants to reach his destination, Its Only possible through the Mapping system. Home to the Meeting, Mapping system coverage is everywhere. Recently google maps discontinue his free API Support and Companies have no idea Read more…