Skip to main content

Building Multi-tenancy Apps

SaaSykit Tenancy is a powerful multi-tenancy starter kit that allows you to build multi-tenancy applications in the easiest ways possible. It comes with a set of features that make building multi-tenancy applications a breeze.

SaaSykit hosts all tenants on a single database, which means that you don't have to maintain multiple databases for each tenant. This makes it easier to maintain and update your application.

But you will need to make sure that your application is multi-tenancy aware, which means that you need to make sure that your application can handle multiple tenants and that each tenant's data is isolated from other tenants using a relation to the Tenant model.

For example, suppose you are building an application that allows your users (tenants) to generate AI articles and each user (tenant) has their own set of articles. In that case, you will need to make sure that each tenant's articles are isolated from other tenant's articles.

You can do that by having a relation between the articles table and the tenants table, where each article belongs to a tenant.

articles (table)
- id
- title
- content
- tenant_id -> foreign key to tenants table

Filament resourcesโ€‹

If you add Filament resources to your tenant panel (dashboard), Filament will automatically look for a tenant_id column in the resource's table when displaying it and filter the resources based on the current tenant (Filament::getTenant()).

This way, you can make sure that each tenant can only see their own resources.

You might also want to incorporate permission checks in case you want certain actions to be done only by certain roles in the tenant. You can check the Roles & Permissions section for more information on how to do that.

Linking data to tenantโ€‹

When creating new record (like an AI article as per the example above) in your application, you will need to make sure that the record is linked to the correct tenant.

One way to do that is to use Laravel Model Events to automatically link the record to the current tenant.

For example, you can use the creating event to automatically link the record to the current tenant:

use Illuminate\Support\Facades\Auth;

class Article extends Model
{
protected static function boot()
{
parent::boot();

static::creating(function ($article) {
$article->tenant_id = Filament::getTenant()->id;
});
}
}
tip

Check the documentation of multi-tenancy on Filament's side to make sure you are following the best practices and understand the range of features that Filament offers for multi-tenancy applications.