How To Connect Database Connection In Laravel - FreeCodeBlog

Last updated: Nov 12, 2021

You must need to connect database connection in laravel for making dynamic laravel website. hello guys, I will explain you how we can connect laravel database connection.

 

How To Connect Database Connection In Laravel
How To Connect Database Connection In Laravel

 

Before continue this tutorial make sure you have already installed any kind version of laravel to practice laravel database connection. If you don't know how to install laravel then read how to install any kind of laravel version tutorial. Now open laravel project with any code editor.

 

Database.php file.

You will get mysql, sqlite, pgsql, sqlsrv database connection in laravel database file.

 

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            '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' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

    ],

 

Connect MySQL Database.

Now open .env file from root folder and database.php file from config folder. After installed new laravel you will see MySQL database connection by default in database and env file.

 

'default' => env('DB_CONNECTION', 'mysql'),

 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

 

Thanks.

Post Tags
Post Writer
masum7323
Masum Biswas (@masum7323)

My name is Masum Biswas. I'm a full stack developer. I live in Bangladesh and I love to write tutorials and tools that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, MySQL, VueJS, Tailwind CSS and Bootstrap from the early stage.

Follow Me On
Related Posts
How To Use Soft Delete In Laravel With Mysql Database

Hi, do you really want to use soft delete to make delete, restore and permanent delete system in laravel for any MySQL database table like posts, p... Read More

Install Laravel On Cpanel Shared Hosting Server Complete Tutorial

Want to install laravel on cpanel? Yes!. You can easily install laravel on shared hosting cpanel server. Want to install laravel 8, 6, 7, 5 version... Read More

Solve Ckeditor 5 Code Block Plugin Not Working In Laravel

Ckeditor 5 code block plugin not working in laravel? No. Do you have a programming laravel project and want to use code block ckeditor 5 plugin in... Read More

Laravel Breeze User Email Verification Complete Tutorial

Did you know that Laravel breeze has a feature called Email Verification? I know little about it. Good, Now you need to know complete information a... Read More

How To Make A Filter Search In Laravel With Vue Js

Laravel vue filter search! Want to make a filter search in laravel with vue js? Yes. Okay don't know to make filter search system using laravel and... Read More