How to configure database in laravel 5.8

In this tutorial, we are going to configure the database in Laravel 5.8 application which we have just installed. If you did not installed app yet then follow tutorial to install Laravel 5.8. Laravel makes it very simple for a developer to configure it. Laravel support four databases

  • MySQL
  • PostgreSQL
  • SQLite
  • SQL Server

MySQL Configuration

Laravel comes with config/database.php file where we are able to configure our database.

'mysql' => [
    'read' => [
        'host' => '192.168.1.1',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
],

There are couple of scenario when we need to use different database connection for different operation. As shown in above configuration code Laravel gives out of the box solution to achieve our goal.

As shown in config file we can configure one database connection for read operation where we can perform only SELECT statements and another database connection for write operation where we can perform INSERT, UPDATE AND DELETE statements.

We are using driver 'driver' => 'mysql', ‘database’ => ‘YOURDATABASENAME’, ‘username’ => ‘YOURMYSQLUSERNAME’, Default username is root is most of the cases. ‘password’ => ‘YOURMYSQLPASSWORD’.

If you have come across the situation where you need to use multiple databases then we can achieve that too with the same config file. you can take a look into our tutorial for configuring multiple databases in Laravel.



References

Laravel Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *