PHP code example of roberts / laravel-singledb-tenancy
1. Go to this page and download the library: Download roberts/laravel-singledb-tenancy 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/ */
roberts / laravel-singledb-tenancy example snippets
use Roberts\LaravelSingledbTenancy\Models\Tenant;
// Create a tenant with a root domain
$tenant = Tenant::create([
'name' => 'Acme Corporation',
'domain' => 'acme.com',
'slug' => 'acme', // Auto-generated if not provided
]);
// Create a tenant with a subdomain
$tenant = Tenant::create([
'name' => 'Beta Company',
'domain' => 'beta.acme.com', // Full subdomain in domain field
'slug' => 'beta',
]);
// Create another tenant with a different subdomain
$tenant = Tenant::create([
'name' => 'Enterprise Division',
'domain' => 'enterprise.acme.com', // Each subdomain gets its own entry
'slug' => 'enterprise',
]);
use Roberts\LaravelSingledbTenancy\Traits\HasTenant;
class Post extends Model
{
use HasTenant;
protected $fillable = ['tenant_id'];
}
// Domain resolution for all routes
Route::middleware(['web', 'tenant'])->group(function () {
Route::get('/dashboard', DashboardController::class);
});
// Domain resolution only
Route::middleware(['web', 'tenant:domain'])->group(function () {
Route::get('/custom', CustomController::class);
});
use Roberts\LaravelSingledbTenancy\Concerns\TenantAware;
class ProcessReport implements ShouldQueue
{
use TenantAware;
public function handle()
{
// All Eloquent queries are automatically scoped to the correct tenant.
$sales = Sale::all();
}
}
use Roberts\LaravelSingledbTenancy\Commands\TenantAwareCommand;
class GenerateReport extends TenantAwareCommand
{
protected $signature = 'report:generate {--tenant=} {--all-tenants}';
protected $description = 'Generate a sales report.';
public function handleTenant()
{
$this->info('Generating report for: ' . current_tenant()->name);
}
}
// Get current tenant
$tenant = current_tenant();
$tenantId = current_tenant_id();
// Check if tenant is set
if (has_tenant()) {
// Tenant-specific logic
}
// Require tenant (throws exception if none set)
$tenant =
// Set tenant context
tenant_context()->set($tenant);
// Clear tenant context
tenant_context()->clear();
// Run without tenant context (see all data)
tenant_context()->runWithout(function () {
$allPosts = Post::all(); // All posts across all tenants
});
// Automatically scoped to current tenant
$posts = Post::all();
// Query specific tenant
$posts = Post::forTenant($tenant)->get();
// Query all tenants (removes tenant scope)
$allPosts = Post::forAllTenants()->get();
// Custom tenant column (override default 'tenant_id')
class CustomModel extends Model
{
use HasTenant;
protected $tenantColumn = 'organization_id';
}
// routes/tenants/acme.com.php
Route::get('/special', ...);
// Also load all the shared routes
tenant_context()->runWithout(function () {
$this->assertCount(10, Post::all()); // All tenant data
});
use Roberts\LaravelSingledbTenancy\Services\SuperAdmin;
$user = auth()->user();
if (app(SuperAdmin::class)->is($user)) {
// User is the super admin
}
use Roberts\LaravelSingledbTenancy\Middleware\AuthorizePrimaryTenant;
Route::get('/tenancy-dashboard', ...)->middleware(AuthorizePrimaryTenant::class);
// Set/get current tenant
tenant_context()->set($tenant);
$tenant = tenant_context()->get();
tenant_context()->clear();
// Run code in tenant context
tenant_context()->runWith($tenant, $callback);
tenant_context()->runWithout($callback);
// Check tenant state
tenant_context()->has();
tenant_context()->id();
// Apply to routes
Route::middleware('tenant')->group(...); // All strategies
Route::middleware('tenant:domain')->group(...); // Domain only
Route::middleware('tenant:subdomain')->group(...); // Subdomain only