gpt4 book ai didi

Laravel Project Multi-Tenancy 、多库、一域

转载 作者:行者123 更新时间:2023-12-02 16:06:30 25 4
gpt4 key购买 nike

你好,你能帮我把项目 laravel Multi-tenant with Multi-database :

我想创建用户数据库,例如在我的系统中有两个用户 A 和 B。我有一个主数据库和两个数据库 user_a(用于用户 A)和 user_b(用于用户 B)。在主数据库中,我有所有用户信息。现在我想要的是,当用户 A 登录系统时,它访问 user_a 数据库,而当用户 B 登录时,数据库连接应该是 user_b 数据库。

最佳答案

  1. 首先,在.env中:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=master_db
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION_A=mysql
DB_HOST_A=127.0.0.1
DB_PORT_A=3306
DB_DATABASE_A=a_db
DB_USERNAME_A=root
DB_PASSWORD_A=

DB_CONNECTION_B=mysql
DB_HOST_B=127.0.0.1
DB_PORT_B=3306
DB_DATABASE_B=b_db
DB_USERNAME_B=root
DB_PASSWORD_B=
  1. config/database.php 中:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

'mysql_a' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_A', '127.0.0.1'),
'port' => env('DB_PORT_A', '3306'),
'database' => env('DB_DATABASE_A', 'forge'),
'username' => env('DB_USERNAME_A', 'forge'),
'password' => env('DB_PASSWORD_A', ''),
'unix_socket' => env('DB_SOCKET_A', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

'mysql_b' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_B', '127.0.0.1'),
'port' => env('DB_PORT_B', '3306'),
'database' => env('DB_DATABASE_B', 'forge'),
'username' => env('DB_USERNAME_B', 'forge'),
'password' => env('DB_PASSWORD_B', ''),
'unix_socket' => env('DB_SOCKET_B', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
  1. 在迁移文件中,如果 users 表存在于主数据库中,而 books 表(例如)同时存在于数据库 A 和 B 中:
Schema::connection('mysql')->create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('database');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::connection('mysql_a')->create('books', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});

Schema::connection('mysql_b')->create('books', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
  1. 现在你有三个数据库连接,当你想改变 ModelDB 上的连接时,你可以这样做:对于模特:
$books = Book::on('mysql_a')->find(1);

对于数据库查询:

DB::connection('mysql_b')->table('books')->get();
  1. 根据用户数据库类型选择书籍的示例:
$user = auth()->user();

switch ($user->database) {
case 'A':
return Book::on('mysql_a')->all();
break;

case 'B':
return Book::on('mysql_b')->all();
break;
}

注意:不要忘记在更改.env 文件或config/database.php 时运行php artisan config:cache

关于Laravel Project Multi-Tenancy 、多库、一域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69303021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com