Multi-tenancy
Introduction
Multi-tenancy is a concept where a single instance of an application serves multiple customers. Each customer has their own data and access rules that prevent them from viewing or modifying each other's data. This is a common pattern in SaaS applications. Users often belong to groups of users (often called teams or organizations). Records are owned by the group, and users can be members of multiple groups. This is suitable for applications where users need to collaborate on data.
Multi-tenancy is a very sensitive topic. It's important to understand the security implications of multi-tenancy and how to properly implement it. If implemented partially or incorrectly, data belonging to one tenant may be exposed to another tenant. Filament provides a set of tools to help you implement multi-tenancy in your application, but it is up to you to understand how to use them.
NOTE
Filament does not provide any guarantees about the security of your application. It is your responsibility to ensure that your application is secure. Please see the security section for more information.
Simple one-to-many tenancy
The term "multi-tenancy" is broad and may mean different things in different contexts. Filament's tenancy system implies that the user belongs to many tenants (organizations, teams, companies, etc.) and may switch between them.
If your case is simpler and you don't need a many-to-many relationship, then you don't need to set up the tenancy in Filament. You could use observers and global scopes instead.
Let's say you have a database column users.team_id, you can scope all records to have the same team_id as the user using a global scope:
use Illuminate\Database\Eloquent\Builder;
class Post extends Model
{
protected static function booted(): void
{
static::addGlobalScope('team', function (Builder $query) {
if (auth()->hasUser()) {
$query->where('team_id', auth()->user()->team_id);
// or with a `team` relationship defined:
$query->whereBelongsTo(auth()->user()->team);
}
});
}
}
To automatically set the team_id on the record when it's created, you can create an observer:
class PostObserver
{
public function creating(Post $post): void
{
if (auth()->hasUser()) {
$post->team_id = auth()->user()->team_id;
// or with a `team` relationship defined:
$post->team()->associate(auth()->user()->team);
}
}
}
Setting up tenancy
To set up tenancy, you'll need to specify the "tenant" (like team or organization) model in the configuration:
use App\Models\Team;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenant(Team::class);
}
You'll also need to tell Filament which tenants a user belongs to. You can do this by implementing the HasTenants interface on the App\Models\User model:
<?php
namespace App\Models;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
class User extends Authenticatable implements FilamentUser, HasTenants
{
// ...
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}
public function getTenants(Panel $panel): Collection
{
return $this->teams;
}
public function canAccessTenant(Model $tenant): bool
{
return $this->teams()->whereKey($tenant)->exists();
}
}
In this example, users belong to many teams, so there is a teams() relationship. The getTenants() method returns the teams that the user belongs to. Filament uses this to list the tenants that the user has access to.

For security, you also need to implement the canAccessTenant() method of the HasTenants interface to prevent users from accessing the data of other tenants by guessing their tenant ID and putting it into the URL.
You'll also want users to be able to register new teams.
Adding a tenant registration page
A registration page will allow users to create a new tenant.
When visiting your app after logging in, users will be redirected to this page if they don't already have a tenant.
To set up a registration page, you'll need to create a new page class that extends Filament\Pages\Tenancy\RegisterTenant. This is a full-page Livewire component. You can put this anywhere you want, such as app/Filament/Pages/Tenancy/RegisterTeam.php:
namespace App\Filament\Pages\Tenancy;
use App\Models\Team;
use Filament\Forms\Components\TextInput;
use Filament\Pages\Tenancy\RegisterTenant;
use Filament\Schemas\Schema;
class RegisterTeam extends RegisterTenant
{
public static function getLabel(): string
{
return 'Register team';
}
public function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('name'),
// ...
]);
}
protected function handleRegistration(array $data): Team
{
$team = Team::create($data);
$team->members()->attach(auth()->user());
return $team;
}
}
You may add any form components to the form() method, and create the team inside the handleRegistration() method.
Now, we need to tell Filament to use this page. We can do this in the configuration:
use App\Filament\Pages\Tenancy\RegisterTeam;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantRegistration(RegisterTeam::class);
}

Customizing the tenant registration page
You can override any method you want on the base registration page class to make it act as you want. Even the $view property can be overridden to use a custom view of your choice.
Adding a tenant profile page
A profile page will allow users to edit information about the tenant.
To set up a profile page, you'll need to create a new page class that extends Filament\Pages\Tenancy\EditTenantProfile. This is a full-page Livewire component. You can put this anywhere you want, such as app/Filament/Pages/Tenancy/EditTeamProfile.php:
namespace App\Filament\Pages\Tenancy;
use Filament\Forms\Components\TextInput;
use Filament\Pages\Tenancy\EditTenantProfile;
use Filament\Schemas\Schema;
class EditTeamProfile extends EditTenantProfile
{
public static function getLabel(): string
{
return 'Team profile';
}
public function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('name'),
// ...
]);
}
}
You may add any form components to the form() method. They will get saved directly to the tenant model.
Now, we need to tell Filament to use this page. We can do this in the configuration:
use App\Filament\Pages\Tenancy\EditTeamProfile;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantProfile(EditTeamProfile::class);
}

Customizing the tenant profile page
You can override any method you want on the base profile page class to make it act as you want. Even the $view property can be overridden to use a custom view of your choice.
Accessing the current tenant
Anywhere in the app, you can access the tenant model for the current request using Filament::getTenant():
use Filament\Facades\Filament;
$tenant = Filament::getTenant();
Billing
Using Laravel Spark
Filament provides a billing integration with Laravel Spark. Your users can start subscriptions and manage their billing information.
To install the integration, first install Spark and configure it for your tenant model.
Now, you can install the Filament billing provider for Spark using Composer:
composer require filament/spark-billing-provider
In the configuration, set Spark as the tenantBillingProvider():
use Filament\Billing\Providers\SparkBillingProvider;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantBillingProvider(new SparkBillingProvider());
}
Now, you're all good to go! Users can manage their billing by clicking a link in the tenant menu.
Requiring a subscription
To require a subscription to use any part of the app, you can use the requiresTenantSubscription() configuration method:
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->requiresTenantSubscription();
}
Now, users will be redirected to the billing page if they don't have an active subscription.
Requiring a subscription for specific resources and pages
Sometimes, you may wish to only require a subscription for certain resources and custom pages in your app. You can do this by returning true from the isTenantSubscriptionRequired() method on the resource or page class:
public static function isTenantSubscriptionRequired(Panel $panel): bool
{
return true;
}
If you're using the requiresTenantSubscription() configuration method, then you can return false from this method to allow access to the resource or page as an exception.
Writing a custom billing integration
Billing integrations are quite simple to write. You just need a class that implements the Filament\Billing\Providers\Contracts\Provider interface. This interface has two methods.
getRouteAction() is used to get the route action that should be run when the user visits the billing page. This could be a callback function, or the name of a controller, or a Livewire component - anything that works when using Route::get() in Laravel normally. For example, you could put in a simple redirect to your own billing page using a callback function.
getSubscribedMiddleware() returns the name of a middleware that should be used to check if the tenant has an active subscription. This middleware should redirect the user to the billing page if they don't have an active subscription.
Here's an example billing provider that uses a callback function for the route action and a middleware for the subscribed middleware:
use App\Http\Middleware\RedirectIfUserNotSubscribed;
use Filament\Billing\Providers\Contracts\BillingProvider;
use Illuminate\Http\RedirectResponse;
class ExampleBillingProvider implements BillingProvider
{
public function getRouteAction(): string
{
return function (): RedirectResponse {
return redirect('https://billing.example.com');
};
}
public function getSubscribedMiddleware(): string
{
return RedirectIfUserNotSubscribed::class;
}
}
Customizing the billing route slug
You can customize the URL slug used for the billing route using the tenantBillingRouteSlug() method in the configuration:
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantBillingRouteSlug('billing');
}
Customizing the tenant menu
The tenant-switching menu is featured in the admin layout. It's fully customizable.
Each menu item is represented by an action, and can be customized in the same way. To register new items, you can pass the actions to the tenantMenuItems() method of the configuration:
use App\Filament\Pages\Settings;
use Filament\Actions\Action;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantMenuItems([
Action::make('settings')
->url(fn (): string => Settings::getUrl())
->icon('heroicon-m-cog-8-tooth'),
// ...
]);
}
Grouping tenant menu items
By default, all tenant menu items are rendered in a single list. If you want to separate them into distinct groups, you can pass an array of arrays to the tenantMenuItems() method. Each array is rendered as its own group, separated by a divider:
use App\Filament\Pages\Members;
use App\Filament\Pages\Settings;
use Filament\Actions\Action;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantMenuItems([
[
Action::make('settings')
->url(fn (): string => Settings::getUrl())
->icon('heroicon-m-cog-8-tooth'),
Action::make('members')
->url(fn (): string => Members::getUrl())
->icon('heroicon-m-user-group'),
],
[
Action::make('documentation')
->url('https://filamentphp.com/docs')
->icon('heroicon-m-book-open'),
],
]);
}

NOTE
The register item is added to the last group by default. To place it yourself, register it explicitly in any group using the register array key. Since its default sort() puts it at the end of its group, adjust the sort if you want it elsewhere within the group:
->tenantMenuItems([
// ...
[
'register' => fn (Action $action): Action => $action->sort(2),
],
])
Allowing the tenants to be searched
You can use the searchableTenantMenu() method in the configuration to allow the tenants to be searched:
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->searchableTenantMenu();
}
This is automatically enabled when there are more than 10 tenants in a user's list. You can disable it using searchableTenantMenu(false).
Customizing the registration link
To customize the registration link in the tenant menu, register a new item with the register array key, and pass a function that customizes the action object:
use Filament\Actions\Action;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantMenuItems([
'register' => fn (Action $action) => $action->label('Register new team'),
// ...
]);
}
Customizing the profile link
To customize the user profile link at the start of the tenant menu, register a new item with the profile array key, and pass a function that customizes the action object:
use Filament\Actions\Action;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantMenuItems([
'profile' => fn (Action $action) => $action->label('Edit team profile'),
// ...
]);
}
Customizing the billing link
To customize the billing link in the tenant menu, register a new item with the profile array key, and pass a function that customizes the action object:
use Filament\Actions\Action
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantMenuItems([
'billing' => fn (Action $action) => $action->label('Manage subscription'),
// ...
]);
}
Conditionally hiding tenant menu items
You can also conditionally hide a tenant menu item by using the visible() or hidden() methods, passing in a condition to check. Passing a function will defer condition evaluation until the menu is actually being rendered:
use Filament\Actions\Action;
Action::make('settings')
->visible(fn (): bool => auth()->user()->can('manage-team'))
// or
->hidden(fn (): bool => ! auth()->user()->can('manage-team'))
Sending a POST HTTP request from a tenant menu item
You can send a POST HTTP request from a tenant menu item by passing a URL to the url() method, and also using postToUrl():
use Filament\Actions\Action;
Action::make('lockSession')
->url(fn (): string => route('lock-session'))
->postToUrl()
Disabling the tenant switcher
By default, users can switch between tenants using the tenant menu. If you want to keep the tenant menu visible but prevent users from switching tenants, you can use the tenantSwitcher() method in the configuration:
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantSwitcher(false);
}
This keeps the tenant menu visible, showing the current tenant name and any custom menu items, but hides the list of other tenants. This is useful when you want to display tenant information without allowing switching, or when switching should be controlled through other means.
Hiding the tenant menu
You can hide the tenant menu by using the tenantMenu(false)
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->tenantMenu(false);
}
However, this is a sign that Filament's tenancy feature is not suitable for your project. If each user only belongs to one tenant, you should stick to simple one-to-many tenancy.



