phpeloquentmulti-tenantlaravel-filament

Filament multi-tenancy cannot find relationship


I'm setting up multi-tenancy in FilamentPhp.

I currently have three models: Organisation, Role and User (and a OrganisationRole pivot model). Organisation and user are many-to-many. This all works as expected when tested via Tinker.

The panel ('admin') has the tenant set:

->tenant(Organisation::class)

The user class implements HasTenants and has the relationship and tenant methods:

public function organisations(): BelongsToMany
{
    return $this->belongsToMany(Organisation::class)
        ->withTimestamps()
        ->withPivot(['level'])
        ->as('access')
        ->using(OrganisationUser::class);
}

public function canAccessTenant(Model $tenant): bool
{
    return $this->organisations()->whereKey($tenant)->exists();
}

public function getTenants(Panel $panel): array|Collection
{
    return $this->organisations;
}

When I log into the panel, I can view the list of roles automatically scoped to the tenant organisation - but when I try to look at the list of organisations, I get the following error message:

The model [App\Models\Organisation] does not have a relationship named [organisation]. You can change the relationship being used by passing it to the [ownershipRelationship] argument of the [tenant()] method in configuration. You can change the relationship being used per-resource by setting it as the [$tenantOwnershipRelationshipName] static property on the [App\Filament\Resources\OrganisationResource] resource class.

I understand that there are situations where I may need to manually define the relationship in use, but I don't understand why Filament is looking for an organisation relationship on the Organisation model.


Solution

  • Fixed it! The OrganisationResource needs to point back to the user relationship:

    protected static ?string $tenantOwnershipRelationshipName = 'users';