Laravel using multiple database connections

Source: Internet
Author: User

1, configuration. env file

Db_connection=mysql
db_host=127.0.0.1
db_port=3306
db_database=database_name
db_username=root
db_password=

db_host_center=127.0.0.1
db_port_center=3306
db_database_center=database_center
DB_USERNAME_CENTER =root
db_password_center=

2. Configure config/database.php

' MySQL ' => [' Driver ' => ' MySQL ', ' Host ' => env (' db_host ', ' localhost '), ' Por T ' => env (' Db_port ', ' 3306 '), ' database ' => env (' db_database ', ' Forge '), ' username ' => en
            V (' db_username ', ' Forge '), ' Password ' => env (' Db_password ', '), ' charset ' => ' UTF8 ', ' Collation ' => ' utf8_unicode_ci ', ' prefix ' => ', ' strict ' => false, ' E Ngine ' => null,], ' mysql_center ' => [' Driver ' => ' MySQL ', ' host ' => Env (' db_host_center ', ' localhost '), ' Port ' => env (' Db_port_center ', ' 3306 '), ' database ' => E  NV (' Db_database_center ', ' Forge '), ' username ' => env (' db_username_center ', ' Forge '), ' password ' 
            => env (' db_password_center ', '), ' charset ' => ' utf8 ', ' collation ' => ' utf8_unicode_ci ', ' Prefix ' =&Gt ', ' strict ' => false, ' engine ' => null,],

3. Create model

1. This model will use the default ' MySQL ' Connection
class Usermodel extends Model
{
    //database ' db ' in the Users table
    protected $ Table = "Users";
}

2. This model will use the Mysql_center connection
class Usermodel extends Model
{
//database ' Dadtabase_center ' in the Users table
    protected $connection = ' mysql_center ';
    Protected $table = "users";

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.