multi-tenant: It doesn't filter everwhere

I'm a newbie at Filament, but have the basic many-to-many relations in my tables working and in the UI. Now I've added multi-tenant, and have it working in the UI for my Resources by including this in the top of each Resource Class: protected static ?string $tenantOwnershipRelationshipName = 'team';. This works well when I am into the UI for reach Resource, but when I am on the default Admin page and have added widgets with this code, it seems to ignore the multi-tenant filter. Or maybe I need to add a Global Scope to filter it? This is the code that is not filtering (eg. shows all records) - any clues?:
class StatsOverview extends BaseWidget
{
protected static ?string $pollingInterval = '30s'; // Update stats
protected static ?string $tenantOwnershipRelationshipName = 'team';

protected function getStats(): array
{
$team_id = Filament::getTenant()->id;

return [
Stat::make('Families', Family::all()->count())
->url("/admin/$team_id/families"),
Stat::make('People', Person::all()->count())
->url("/admin/$team_id/people"),
Stat::make('Properties', Property::all()->count())
->url("/admin/$team_id/properties"),
];
}
}
class StatsOverview extends BaseWidget
{
protected static ?string $pollingInterval = '30s'; // Update stats
protected static ?string $tenantOwnershipRelationshipName = 'team';

protected function getStats(): array
{
$team_id = Filament::getTenant()->id;

return [
Stat::make('Families', Family::all()->count())
->url("/admin/$team_id/families"),
Stat::make('People', Person::all()->count())
->url("/admin/$team_id/people"),
Stat::make('Properties', Property::all()->count())
->url("/admin/$team_id/properties"),
];
}
}
4 Replies
awcodes
awcodes3w ago
If I’m understanding correctly you’re assuming a tenant id on the widget but widgets don’t have a tenant id so you need to handle that explicitly in the stat queries.
Stricks
Stricks3w ago
Oh, so when I call Stat::make('Families', Family::all()->count()) I would have assumed the ::all() would have been filtered. Is there a way to filter on tenant everywhere or is left up to the coder to do that? When I've done this with previous SaaS systems I wrote, I had to do all the filtering on each SQL call. Filament looks like it can do this at a low level, thus eliminating the inevitable mistake by me when coding.
awcodes
awcodes3w ago
The Stat class doesn’t know anything about your tenant set up so you need to handle the query appropriately.
Stricks
Stricks3w ago
Thanks! I got the stats filtering on tenant - pretty easy. I am slowly learning Filament 🙂 On a related note, when you go to Attach one record to another, the pulldown there displays all the records, not the ones filtered on Tenant. And to be clear, I'm trying to use Filament's Tenant docs, and avoiding scoping if possible as doing a JOIN though scoping (family -> family_teams -> teams) seems awkward at best.
Want results from more Discord servers?
Add your server