F
Filament17mo ago
N1XN

Scopes not applied to relations in getEloquentQuery

In my case I have Tenant and Address models that are connected with each other. The Address has tenant_id so a user can only display its own addresses through his own tenant. I have a TenantResource only for admins where all tenants are listed including their address and country code. My issue is that in the code below the getEloquentQuery global scopes get ignored but when defined in the model it works. Isn't this actually the same?
<?php

class Tenant extends BaseTenant
{
public function address(): HasOne
{
// DOES NOT WORK
// return $this->hasOne(Address::class)->oldest();

// WORKS
return $this->hasOne(Address::class)->oldest()->withoutGlobalScopes();
}
}


<?php

class TenantResource extends SuperAdminResource
{
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->with(['address' => function ($query) {
$query->withoutGlobalScope(TenantScope::class)->with('country');
}]);
}
}
<?php

class Tenant extends BaseTenant
{
public function address(): HasOne
{
// DOES NOT WORK
// return $this->hasOne(Address::class)->oldest();

// WORKS
return $this->hasOne(Address::class)->oldest()->withoutGlobalScopes();
}
}


<?php

class TenantResource extends SuperAdminResource
{
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->with(['address' => function ($query) {
$query->withoutGlobalScope(TenantScope::class)->with('country');
}]);
}
}
1 Reply
N1XN
N1XNOP17mo ago
Sorry, this was not solved. I tried it acutally with tinker and in tinker it works, its just the getEloquentQuery() that ignores this stuff...

Did you find this page helpful?