在10月CMS中连接到不同的数据库

Connecting to Different Database in October CMS

Hello,

I have two databases first one was created by OctoberCMS during installation process and the other one which i had already. how do i connect to that other database, i tried using builder plug-in but i am confused how to do it.

I want to connect to the other database and fetch information from one table and display on my home page.

Please help me with this. I am newbie to this CMS.

Thank you.

In your /config/database.php you will have something like this (assuming MySQL):

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'port'      => '3306',
        'database'  => 'database_name',
        'username'  => 'username',
        'password'  => 'password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ],

Just add another one after it, changing the name to something else (mysql-legacy in my example below, before the pgsql configuration) and the relevant connection details:

    'mysql-legacy' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'port'      => '3306',
        'database'  => 'database_name',
        'username'  => 'username',
        'password'  => 'password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ],

After that, you can use DB; in your model or controller and then do things like this:

$query = DB::connection('mysql-legacy')->select('SELECT * FROM tablename');