承接 kashifleo/multi-db-bridge 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kashifleo/multi-db-bridge

Composer 安装命令:

composer require kashifleo/multi-db-bridge

包简介

A Laravel package for managing central and tenant databases simultaneously using explicit, secure tenant connections.

README 文档

README

DbBridge is a Laravel package for managing central and tenant databases simultaneously using explicit, secure tenant connections.

Features

  • Central-Managed Tenancy: Single central database storing tenant credentials.
  • Explicit Connection: No auto-magic middleware. You control when to connect.
  • Simultaneous Access: Query User::on('central') and Order::on('tenant') at the same time.
  • Secure: Credentials stored in DB, not separate .env files.
  • Job Support: Full support for queues and jobs with automatic tenant context preservation.

Installation

composer require kashifleo/multi-db-bridge

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Kashifleo\MultiDBBridge\DbBridgeServiceProvider"

This will create config/dbbridge.php:

return [
    'central_connection' => 'mysql', // Your main connection
    'tenant_connection' => 'tenant', // The dynamic connection name
    'tenant_model' => App\Models\Tenant::class, // Your Tenant Model
    'tenant_migrations_path' => 'database/migrations/tenants', // Path to tenant migrations
    'tenant_database_prefix' => 'tenant_', // Prefix for tenant databases
];

Usage

Tenant Model

Your App's Tenant model must implement Kashifleo\MultiDBBridge\Contracts\DbBridgeConnectionContract:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Kashifleo\MultiDBBridge\Contracts\DbBridgeConnectionContract;

class Tenant extends Model implements DbBridgeConnectionContract
{
    public function getDatabaseDriver(): string { return $this->db_driver; }
    public function getDatabaseHost(): string { return $this->db_host; }
    public function getDatabasePort(): int { return $this->db_port; }
    public function getDatabaseName(): string { return $this->db_name; }
    public function getDatabaseUsername(): string { return $this->db_username; }
    public function getDatabasePassword(): string { return $this->db_password; }
}
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Kashifleo\MultiDBBridge\Contracts\DbBridgeConnectionContract;

class Tenant extends Model implements DbBridgeConnectionContract
{
    // Tenant connection name defined
    protected $connection  = 'tenant';

    public function getDatabaseDriver(): string { return $this->db_driver; }
    public function getDatabaseHost(): string { return $this->db_host; }
    public function getDatabasePort(): int { return $this->db_port; }
    public function getDatabaseName(): string { return $this->db_name; }
    public function getDatabaseUsername(): string { return $this->db_username; }
    public function getDatabasePassword(): string { return $this->db_password; }
}
// Query Tenant DB
$orders = \App\Models\Order::find(1);

Database Management

You can programmatically manage tenant databases using the DbBridge facade. This is useful for onboarding flows.

use Kashifleo\MultiDBBridge\Facades\DbBridge;

// Generate a standard database name
// Pattern: {prefix}{id}_{slug}_{year}
$dbName = DbBridge::generateDatabaseName($tenant);
$tenant->update(['db_database' => $dbName]);

// Create the tenant's database
// This uses the 'tenant_database_prefix' from config if you use it in your model
DbBridge::createDatabase($tenant);

// Run migrations for the tenant
DbBridge::migrate($tenant);

// Drop the tenant's database (careful!)
DbBridge::dropDatabase($tenant);

Connecting to a Tenant

use Kashifleo\MultiDBBridge\Facades\DbBridge;
use App\Models\Tenant;

$tenant = Tenant::find(1);

// Connect explicitly
DbBridge::connect($tenant);

// Check connection
if (DbBridge::isConnected()) {
    $current = DbBridge::current();
}

// Disconnect
DbBridge::disconnect();

Simultaneous Database Usage

// Query Central DB (default connection)
$users = \App\Models\User::on('mysql')->get(); // or default

// Query Tenant DB
$orders = \App\Models\Order::on('tenant')->get();

Tenant Database Migrations

This package provides a robust way to manage tenant database migrations separate from your central migrations.

Configuration

Ensure your config/dbbridge.php has the migrations path configured:

'tenant_migrations_path' => 'database/migrations/tenants',

Creating Tenant Migrations

Use the dbbridge:make-migration command to create a migration file specifically for tenant databases. These files will be placed in the configured tenant migrations path.

# Create a new table
php artisan dbbridge:make-migration create_orders_table --create=orders

# Add a column to an existing table
php artisan dbbridge:make-migration add_status_to_orders_table --table=orders

Running Tenant Migrations

Use the dbbridge:migrate command to run migrations on tenant databases.

Migrate a Single Tenant:

php artisan dbbridge:migrate --id=1

Migrate All Tenants:

php artisan dbbridge:migrate --all

The command dynamically connects to each tenant's database using the credentials stored in your central database and runs the migrations found in the tenant_migrations_path.

Middleware Usage

The EnsureDbBridgeConnected middleware acts as a Guard. It aborts the request with a 403 Unauthorized error if no tenant is connected. It does not automatically connect for you; it only ensures security.

Registering the Middleware

As this is a package, you must register the middleware in your application.

For Laravel 11+ (bootstrap/app.php):

use Kashifleo\MultiDBBridge\Middleware\EnsureDbBridgeConnected;

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'tenant.auth' => EnsureDbBridgeConnected::class,
    ]);
})

Applying to Routes

Use it on routes that strictly require a tenant context (e.g., dashboard, orders).

Route::middleware([
    'web', 
    'auth', 
    'tenant.auth' // Ensures a tenant is connected before proceeding
])->group(function () {
    
    Route::get('/dashboard', function () {
        return DbBridge::current()->name . ' Dashboard';
    });
    
});

Queue Support

To use tenant connections inside Queued Jobs, you should pass the Tenant model to the Job's constructor and explicitly connect within the handle method. This ensures clarity and control.

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Kashifleo\MultiDBBridge\Facades\DbBridge;
use App\Models\Tenant as TenantModel;

class ProcessTenantOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $tenant;

    public function __construct(TenantModel $tenant)
    {
        $this->tenant = $tenant;
    }

    public function handle()
    {
        // Explicitly connect to the tenant
        DbBridge::connect($this->tenant);

        // Perform actions on the tenant database
        // ...

        // Optional: Disconnect if needed, though the worker will likely reset for next job
        // DbBridge::disconnect();
    }
}

License

MIT

kashifleo/multi-db-bridge 适用场景与选型建议

kashifleo/multi-db-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kashifleo/multi-db-bridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kashifleo/multi-db-bridge 我们能提供哪些服务?
定制开发 / 二次开发

基于 kashifleo/multi-db-bridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 69
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-17