Multi-Tenancy
Tapix includes built-in support for multi-tenant applications. When enabled, all imports are automatically scoped to the current tenant, and tenant context is preserved across queue jobs.
Configuration
Enable multi-tenancy in the Tapix config file:
'tenant' => [
'enabled' => false,
'column' => 'tenant_id',
'model' => null,
],
| Option | Default | Description |
|---|---|---|
enabled | false | Whether tenant scoping is active. |
column | tenant_id | The foreign key column on the imports table. |
model | null | The tenant model class (e.g., App\Models\Team). |
Custom Tenant Resolver
By default, Tapix attempts to resolve the tenant from Filament's panel tenant. For custom resolution logic, register a resolver in a service provider:
use Tapix\Core\Tapix;
Tapix::resolveTenantUsing(function () {
return auth()->user()?->current_team_id;
});
The resolver should return the tenant ID (integer or string) for the current request context.
TenantContextService
The TenantContextService manages tenant state throughout the request lifecycle and across queue jobs. It provides the following methods:
Setting the Tenant
use Tapix\Core\Services\TenantContextService;
// Set a custom resolver closure
$service->setTenantResolver(function () {
return auth()->user()?->current_team_id;
});
// Set the tenant ID directly
$service->setTenantId(42);
// Set from the current Filament panel tenant
$service->setFromFilamentTenant();
Reading the Tenant
// Resolve the current tenant ID
// Resolution order: custom resolver -> direct context -> Filament tenant
$tenantId = $service->getCurrentTenantId();
// Check if a tenant context exists
if ($service->hasTenantContext()) {
// ...
}
Temporary Context Switching
Use withTenant() to execute a closure within a different tenant context:
$service->withTenant($otherTenantId, function () {
// All queries and operations here are scoped to $otherTenantId
});
// Original tenant context is restored after the closure
Automatic Query Scoping
When tenancy is enabled, the TenantScope global scope is applied to the Import model. This ensures that all Eloquent queries on imports are automatically filtered by the current tenant:
// Only returns imports belonging to the current tenant
$imports = Import::all();
No manual where clauses are needed. The scope is applied transparently.
Filament Panel Integration
When your Filament panel has tenancy configured, TapixPlugin automatically registers the SetTenantContextMiddleware. This middleware sets the tenant context at the beginning of each request, so imports created through the panel are always associated with the correct tenant.
TapixPlugin in a Filament panel with tenancy, tenant context middleware is registered automatically. No additional setup is required beyond enabling tenancy in the Tapix config and configuring your panel's tenancy as usual.No additional setup is required beyond enabling tenancy in the Tapix config and configuring your Filament panel's tenancy as usual.
Queue Jobs
Import queue jobs use the TenantAware trait to serialize and restore tenant context. When a job is dispatched, the current tenant ID is captured. When the job executes on a worker, the tenant context is restored before any database operations run.
This ensures that background processing respects tenant boundaries, even when jobs are processed by workers that have no request context.