Working with Models
Filament Tenancy provides special traits for working with models in multi-tenant context.
Available Traits
Section titled “Available Traits”BelongsToTenant
Section titled “BelongsToTenant”For models that belong to a specific tenant:
use AngelitoSystems\FilamentTenancy\Concerns\BelongsToTenant;use Illuminate\Database\Eloquent\Model;
class Post extends Model{ use BelongsToTenant;
// Your model code...}This trait ensures that queries only return data from the current tenant.
UsesLandlordConnection
Section titled “UsesLandlordConnection”For models that always use the central database (landlord):
use AngelitoSystems\FilamentTenancy\Concerns\UsesLandlordConnection;use Illuminate\Database\Eloquent\Model;
class Plan extends Model{ use UsesLandlordConnection;
// Your model code...}This trait ensures that the model always uses the central connection, regardless of tenant context.
HasRoles
Section titled “HasRoles”For models with roles and permissions:
use AngelitoSystems\FilamentTenancy\Concerns\HasRoles;use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable{ use HasRoles;
// Your model code...}See Manage Users for more details on roles and permissions.
Switch Tenant Context
Section titled “Switch Tenant Context”Using the Facade
Section titled “Using the Facade”use AngelitoSystems\FilamentTenancy\Facades\Tenancy;use AngelitoSystems\FilamentTenancy\Models\Tenant;
// Switch to a specific tenant$tenant = Tenant::find(1);Tenancy::switchToTenant($tenant);
// Now all queries use the tenant's database$users = User::all(); // Tenant users
// Return to central contextTenancy::switchToCentral();
// Now queries use the central database$tenants = Tenant::all(); // All tenantsExecute Code in Tenant Context
Section titled “Execute Code in Tenant Context”use AngelitoSystems\FilamentTenancy\Facades\Tenancy;
Tenancy::runForTenant($tenant, function () { // This code runs in tenant context $user = User::create([ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => bcrypt('password'), ]);
// Context automatically returns to central after});Tenant Model
Section titled “Tenant Model”Get Current Tenant
Section titled “Get Current Tenant”use AngelitoSystems\FilamentTenancy\Facades\Tenancy;
$currentTenant = Tenancy::current();
if ($currentTenant) { echo "Current tenant: " . $currentTenant->name;}Get Full Domain
Section titled “Get Full Domain”$tenant = Tenant::find(1);
// Get full domain (uses APP_DOMAIN if configured)$fullDomain = $tenant->getFullDomain();// Example: "acme.example.com"
// Get full URL$url = $tenant->getUrl();// Example: "https://acme.example.com"Additional Data (JSON)
Section titled “Additional Data (JSON)”The Tenant model has a data field to store additional information:
$tenant->data = [ 'settings' => [ 'theme' => 'dark', 'timezone' => 'UTC', ], 'features' => ['feature1', 'feature2'],];$tenant->save();
// Access data$theme = $tenant->data['settings']['theme'] ?? 'light';Relationships
Section titled “Relationships”Relationships with Plan
Section titled “Relationships with Plan”// Get tenant's plan$plan = $tenant->plan;
// Get active subscription$subscription = $tenant->subscription;Relationships with Subscription
Section titled “Relationships with Subscription”// Get all tenant subscriptions$subscriptions = $tenant->subscriptions;
// Get active subscription$activeSubscription = $tenant->activeSubscription;Next Steps
Section titled “Next Steps”- Configure middleware for additional security
- Review Filament resources for admin panels