码迷,mamicode.com
首页 > 数据库 > 详细

使用laravel框架的eloquent模型连接多个数据库

时间:2017-02-20 10:53:33      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:tab   protected   logs   pre   sql   数据   htm   local   localhost   

1、配置.env文件

DB_HOST_TRAILER=127.0.0.1
DB_PORT_TRAILER=3306
DB_DATABASE_TRAILER=htms_trailer
DB_USERNAME_TRAILER=root
DB_PASSWORD_TRAILER=

DB_HOST_FREIGHT=127.0.0.1
DB_PORT_FREIGHT=3306
DB_DATABASE_FREIGHT=hangli_saas
DB_USERNAME_FREIGHT=root
DB_PASSWORD_FREIGHT=

 2、配置config/database.php

‘mysql_freight‘ => [
            ‘driver‘    => ‘mysql‘,
            ‘host‘      => env(‘DB_HOST_FREIGHT‘, ‘localhost‘),
            ‘database‘  => env(‘DB_DATABASE_FREIGHT‘, ‘forge‘),
            ‘username‘  => env(‘DB_USERNAME_FREIGHT‘, ‘forge‘),
            ‘password‘  => env(‘DB_PASSWORD_FREIGHT‘, ‘‘),
            ‘charset‘   => ‘utf8‘,
            ‘collation‘ => ‘utf8_unicode_ci‘,
            ‘prefix‘    => ‘‘,
            ‘strict‘    => false,
            ‘engine‘    => null,
        ],
        ‘mysql_trailer‘ => [
            ‘driver‘    => ‘mysql‘,
            ‘host‘      => env(‘DB_HOST_TRAILER‘, ‘localhost‘),
            ‘database‘  => env(‘DB_DATABASE_TRAILER‘, ‘forge‘),
            ‘username‘  => env(‘DB_USERNAME_TRAILER‘, ‘forge‘),
            ‘password‘  => env(‘DB_PASSWORD_TRAILER‘, ‘‘),
            ‘charset‘   => ‘utf8‘,
            ‘collation‘ => ‘utf8_unicode_ci‘,
            ‘prefix‘    => ‘‘,
            ‘strict‘    => false,
            ‘engine‘    => null,
        ],

  3、修改model

//添加
protected $connection = mysql_freight;

 

使用laravel框架的eloquent模型连接多个数据库

标签:tab   protected   logs   pre   sql   数据   htm   local   localhost   

原文地址:http://www.cnblogs.com/blog-dyn/p/6418204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!