Video Version of the Lesson
[Only for premium members]
[Only for premium members]
[Only for premium members]
Filament 3 comes with multi-tenancy support out of the box: the screenshot below shows how you can switch between teams/companies, see on the top-left:
This lets you quickly set up a multi-tenant application within a single database, just by configuring the panel. It even takes care of the switching between tenants for you.
It is important to note here that the meaning of multi-tenancy
is different for everyone and that the demo in this lesson is just one of the possible implementations. It is not a one-size-fits-all solution. And the tenancy implementation also depends on your custom code more than on Filament core functionality.
Filament documentation says this:
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.
With that said, here's our approach:
tenant
Model. For example, Company, Organization, Team, etc.belongsToMany
relationship with that Tenant model. In other words, user may belong to many teams.PanelProvider
to indicate that this panel is multi-tenant.tenant_id
column, or an alternative, like company_id
, organization_id
, team_id
, etc.First, we have to create a multi-tenancy model, which in our case will be Company
:
Migration
Schema::create('companies', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps();}); Schema::create('company_user', function (Blueprint $table) { $table->foreignId('company_id')->constrained(); $table->foreignId('user_id')->constrained();});
app/Models/Company.php
use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Company extends Model{ protected $fillable = [ 'name' ]; public function users(): BelongsToMany { return $this->belongsToMany(User::class); }}
Once we have our Company
model, we can tell Filament that our panel is...