1. Go to this page and download the library: Download kashifleo/multi-db-bridge library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kashifleo / multi-db-bridge example snippets
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
];
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);
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);
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();
// Query Central DB (default connection)
$users = \App\Models\User::on('mysql')->get(); // or default
// Query Tenant DB
$orders = \App\Models\Order::on('tenant')->get();
Route::middleware([
'web',
'auth',
'tenant.auth' // Ensures a tenant is connected before proceeding
])->group(function () {
Route::get('/dashboard', function () {
return DbBridge::current()->name . ' Dashboard';
});
});
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();
}
}