Multi-Tenancy with a Single Team per User

Multi-tenancy is often looked at as a many-to-many thing. Users can belong to many teams. But what if your requirements include Users belonging to just one team? This example shows just that.

FilamentExamples ThumbBase (33)

Get the Source Code:

How it works

Teams are based on Filament native tenant support. User can belong to only one team. To make it happen, two interfaces were added to the User Model:

  1. HasDefaultTenant
  2. HasTenants

The most important method here is getDefaultTenant() to set the active tenant. If it is set, Filament will know that the user already has a tenant every time they log in.

app/Models/User.php:

use Filament\Models\Contracts\HasTenants;
use Filament\Models\Contracts\HasDefaultTenant;
 
class User extends Authenticatable implements FilamentUser, HasDefaultTenant, HasTenants
{
// ...
 
public function team(): BelongsTo
{
return $this->belongsTo(Team::class);
}
 
public function teamOwner(): HasOne
{
return $this->hasOne(Team::class, 'owner_id');
}
// ...
The FULL tutorial is available after the purchase: in the Readme file of the official repository you would get invited to.
Get the Source Code: All 61 Premium Examples for $99