Filament Tenancy Help

I have a DocumentResource in an app with tenancy. The document can be associated with a Team ID or it can be null to represent a document that can be accessed by all Teams. How would I then modify the query for the filament table to return the null records on top of the default view which is shown based on current tenancy. I tried the following thinking I was a Laravel genius but of course this doesn't work when filters are applied.
->modifyQueryUsing(function($query) {
$query->orWhere('team_id', null);
})
->modifyQueryUsing(function($query) {
$query->orWhere('team_id', null);
})
Would appreciate any help.
4 Replies
awcodes
awcodes8mo ago
Return $query
Hightower
HightowerOP8mo ago
Didnt help, returned $query, but as soon as I put a filter on the filter doesnt apply properly. Applies fine for records belonging to the team id, but then the table is also full of all null ids instead of also applying filter to those
Sjoerd24
Sjoerd248mo ago
I had the same idea in my project for some pages. I solved it by adding this in the resource itself:
protected static bool $isScopedToTenant = false;
protected static bool $isScopedToTenant = false;
And then scoping the query in the middleware (applyTenantScopes, also recommended in the docs) Sth like this:
Modelname::addGlobalScope(
fn(Builder $query) => $query
->where('team_id','=', null)
->orWhere('team_id', '=', Filament::getTenant()->id),
);
Modelname::addGlobalScope(
fn(Builder $query) => $query
->where('team_id','=', null)
->orWhere('team_id', '=', Filament::getTenant()->id),
);
Hope that helps you!
Hightower
HightowerOP8mo ago
Cheers, I figured it out in a similar way just before you replied. Using the same scopeToTenant set to false, then my query in the table field as above started working

Did you find this page helpful?