Disable Filament Tenancy Scoping for a Model

Is there any way to disable filament automatic tenancy scoping for a model? Take this Role model for an example,
class Role extends SpatieRole
{
public function organization(): BelongsTo
{
return $this->belongsTo(Organization::class);
}

public function newQuery()
{
// Ignore Filament's tenancy scope for any Role query
return parent::newQuery()
->when(
function_exists('filament') && filament(),
fn($q) =>
$q->withoutGlobalScope(filament()->getTenancyScopeName())
);
}
}
class Role extends SpatieRole
{
public function organization(): BelongsTo
{
return $this->belongsTo(Organization::class);
}

public function newQuery()
{
// Ignore Filament's tenancy scope for any Role query
return parent::newQuery()
->when(
function_exists('filament') && filament(),
fn($q) =>
$q->withoutGlobalScope(filament()->getTenancyScopeName())
);
}
}
Without the newQuery function, it automatically scopes using the app_tenancy (from getTenancyScopeName()). While that is okay for normal use-case, my specific use case was to use global roles (using Shield/laravel-permissions) Although, I am not sure if this is the most pragmatic way of doing things. RoleResource::scopeToTenant(false) is not working, so this was the only way I have been able to use to solve my problem. My solution was in the docs too: https://filamentphp.com/docs/4.x/users/tenancy#tenancy-security
Solution:
I have tried that using
protected static bool $isScopedToTenant = false;
protected static bool $isScopedToTenant = false;
but to no avail. I have been only able to use the newQuery to fix it....
Jump to solution
2 Replies
Dennis Koch
Dennis Koch4d ago
RoleResource::scopeToTenant(false) is not working, so this was the only way I have been able to use to solve my problem.
You can just declare the property on the resource class, right?
Solution
kcniverba-mis
kcniverba-mis3d ago
I have tried that using
protected static bool $isScopedToTenant = false;
protected static bool $isScopedToTenant = false;
but to no avail. I have been only able to use the newQuery to fix it. Another fix as well is not to use Filament Shield, and the Filament's global scope would not apply automatically, not sure what's the difference of using the Shield compared to the Spatie permissions package but it works. Will mark as resolved for now.

Did you find this page helpful?