PHP code example of quantumtecnology / multi-tenant
1. Go to this page and download the library: Download quantumtecnology/multi-tenant 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/ */
quantumtecnology / multi-tenant example snippets
$tenant = tenant(); // returns the current tenant bound in the container, or null
$tenant = app('tenant');
use QuantumTecnology\Tenant\Support\TenantManager;
// Resolve your Tenant model from config
$model = config('tenant.model.tenant');
// Locate the tenant you want to work with
$tenant = $model::query()->firstWhere((new $model())->getTenantKeyName(), $tenantId);
if ($tenant) {
// Switch the application context to this tenant
app(TenantManager::class)->switchTo($tenant);
// ... run your tenant-specific logic here ...
// e.g. models, DB, cache, etc., will use the tenant context
// Optionally, when finished, return to the central context
app(TenantManager::class)->disconnect();
}
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use QuantumTecnology\Tenant\Support\TenantManager;
class IdentifyTenantMiddleware
{
public function handle(Request $request, Closure $next)
{
$model = config('tenant.model.tenant');
// Strategy 1: subdomain (e.g., {tenant}.your-app.com)
$subdomain = explode('.', $request->getHost())[0] ?? null;
// Strategy 2: header (e.g., X-Tenant-ID)
$headerId = $request->header('X-Tenant-ID');
// Strategy 3: route parameter (e.g., /tenants/{tenant}/dashboard)
$routeId = $request->route('tenant');
$keyName = (new $model())->getTenantKeyName();
$tenant = $model::query()
->when($routeId, fn ($q) => $q->orWhere($keyName, $routeId))
->when($headerId, fn ($q) => $q->orWhere($keyName, $headerId))
->when($subdomain, fn ($q) => $q->orWhere('slug', $subdomain)) // assuming you have a 'slug' column
->first();
if ($tenant) {
app(TenantManager::class)->switchTo($tenant);
}
try {
return $next($request);
} finally {
// Ensure we return to the central context after the request
app(TenantManager::class)->disconnect();
}
}
}
// Single route
Route::get('/dashboard', DashboardController::class)->middleware('tenant');
// Or a route group
Route::middleware(['tenant'])->group(function () {
Route::get('/reports', ReportsController::class);
Route::get('/invoices', InvoicesController::class);
});
use QuantumTecnology\Tenant\Contracts\TenantConnectionResolver;
use QuantumTecnology\Tenant\Models\Tenant;
class MyResolver implements TenantConnectionResolver
{
public function buildConnectionConfig(Tenant $tenant, array $base, array $dataTenant): array
{
return array_merge($base, [
'database' => $tenant->database,
'username' => $tenant->db_user,
'password' => $tenant->db_pass,
]);
}
public function connectionName(Tenant $tenant): string
{
return 'tenant';
}
}
use QuantumTecnology\Tenant\Contracts\TenantConnectionResolver;
$this->app->singleton(TenantConnectionResolver::class, MyResolver::class);
if ($tenant = tenant()) {
tenantLogAndPrint("Running for tenant {$tenant->id}");
}
use QuantumTecnology\Tenant\Models\Concerns\CentralConnection; // central connection alias
class AuditLog extends Model
{
use CentralConnection; // forces connection name 'central'
}