toanld/laravel-model

该软件包最新版本(1.1.2)的许可证信息不可用。

Laravel模型支持多连接格式的主从。通过权重随机连接一个从服务器

1.1.2 2019-10-15 06:49 UTC

This package is auto-updated.

Last update: 2024-09-17 14:53:36 UTC


README

Vật Giá Laravel (Lumen) 模型帮助使用主从数据库的多连接

功能

  • 通过主从模式连接多个数据库
  • 随机连接一个配置了权重的从数据库

使用方法

配置数据库文件如下

<?php

return [

    'default' => 'master',

    'connections' => [
        'master' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
        ],
        'slaves' => [
            'web31' => [
                'driver' => 'mysql',
                'read' => [
                    'host' => env('DB31_HOST'),
                ],
                'write' => [
                    'host' => env('DB_HOST'),
                ],
                'port' => env('DB_PORT'),
                'database' => env('DB_DATABASE'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                'charset' => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
                'strict' => false,
                'weight' => 100,
            ],
            'web32' => [
                'driver' => 'mysql',
                'read' => [
                    'host' => env('DB32_HOST'),
                ],
                'write' => [
                    'host' => env('DB_HOST'),
                ],
                'port' => env('DB_PORT'),
                'database' => env('DB_DATABASE'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                'charset' => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
                'strict' => false,
                'weight' => 50,
            ],
            'web33' => [
                'driver' => 'mysql',
                'read' => [
                    'host' => env('DB33_HOST'),
                ],
                'write' => [
                    'host' => env('DB_HOST'),
                ],
                'port' => env('DB_PORT'),
                'database' => env('DB_DATABASE'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                'charset' => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
                'strict' => false,
                'weight' => 50,
            ],
            'web34' => [
                'driver' => 'mysql',
                'read' => [
                    'host' => env('DB34_HOST'),
                ],
                'write' => [
                    'host' => env('DB_HOST'),
                ],
                'port' => env('DB_PORT'),
                'database' => env('DB_DATABASE'),
                'username' => env('DB_USERNAME'),
                'password' => env('DB_PASSWORD'),
                'charset' => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
                'strict' => false,
                'weight' => 50,
            ],
        ],
    ],
];

创建的模型需要继承自 class VatGia\Model\Model

如何更改数据库

News::setConnection('master')
News::setConnection('slavse')
News::setConnection('slavse.web31')