douna4762 2017-07-03 07:01
浏览 41

在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.

  • 写回答

1条回答 默认 最新

  • douyi02577 2017-07-03 07:17
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决