Skip to content

Working with Models

Filament Tenancy provides special traits for working with models in multi-tenant context.

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.

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.

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.

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 context
Tenancy::switchToCentral();
// Now queries use the central database
$tenants = Tenant::all(); // All tenants
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
});
use AngelitoSystems\FilamentTenancy\Facades\Tenancy;
$currentTenant = Tenancy::current();
if ($currentTenant) {
echo "Current tenant: " . $currentTenant->name;
}
$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"

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';
// Get tenant's plan
$plan = $tenant->plan;
// Get active subscription
$subscription = $tenant->subscription;
// Get all tenant subscriptions
$subscriptions = $tenant->subscriptions;
// Get active subscription
$activeSubscription = $tenant->activeSubscription;