laravel使用mysql分库说明

戚鸿
2023-12-01
laravel分库说明:
1、修改配置文件.env 加上:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=hoteldb
    DB_DATABASE_CENTER=hotel_data_db  // 多加的一条
    DB_USERNAME=root
    DB_PASSWORD=

2、修改配置文件conf/database.php 加上:
    'connections' => [
        'hotel_data_db' => [    //加在connections数组中
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => '3306',
            'database' => 'hotel_data_db',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
    ]

3、控制器中使用事例:
    use Illuminate\Support\Facades\DB;
    use Illuminate\Database\Connection;
    
    $data = DB::connection('hotel_data_db')->table('hotel_travel_type_structure_tb')
                    ->select(DB::connection('hotel_data_db')->raw('sum(cust_num) as cust_num,travel_type'))
                    ->get();
 类似资料: