Filament not pulling from latest_team_id when coming back to the panel that has tenancy.

I've added the HasDefaulTenant to my user model as well as the methods described in the docs, but it doesn't seem to do anything. I was able to set the current team when a user selects it by adding it to my ApplyTenantScopes middleware. So when I select a team in my app panel, it's stored in the DB. However, if I go to my admin panel (no tenancy), and then come back to my app panel, it's always just going to the first team. It doesn't seem to be taking into account the latest_team_id on the user model. My AppPanelProvider:
->tenant(Team::class, ownershipRelationship: 'team')
->tenantMiddleware([
ApplyTenantScopes::class,
], isPersistent: true)
->tenant(Team::class, ownershipRelationship: 'team')
->tenantMiddleware([
ApplyTenantScopes::class,
], isPersistent: true)
My user model:

public function getDefaultTenant(Panel $panel): ?Model
{
return $this->latestTeam;
}

public function latestTeam(): BelongsTo
{
return $this->belongsTo(Team::class, 'latest_team_id');
}

public function getDefaultTenant(Panel $panel): ?Model
{
return $this->latestTeam;
}

public function latestTeam(): BelongsTo
{
return $this->belongsTo(Team::class, 'latest_team_id');
}
2 Replies
josef
josef13mo ago
You sure that latest_team_id is updated correctly? I'd also advise you to not make the middleware persistent, as ajax requests don't have the tenant in the URL, so it can't be determined. Might also be source of the problem, depending on how you implemented the middleware. Care to share the code?
Jon Mason
Jon MasonOP13mo ago
Yeah, this my ApplyTenantScopes class:
class ApplyTenantScopes
{
public function handle(Request $request, Closure $next)
{
if (Filament::getCurrentPanel()->getId() == 'admin') {
return $next($request);
}

if ($request->getPathInfo() === '/quickbooks/connect') {
return $next($request);
}

Location::addGlobalScope(
fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()),
);


if (Filament::getTenant()->id !== Auth::user()->latest_team_id) {
Auth::user()->latest_team_id = Filament::getTenant()->id;
Auth::user()->save();
}

return $next($request);
}
}
class ApplyTenantScopes
{
public function handle(Request $request, Closure $next)
{
if (Filament::getCurrentPanel()->getId() == 'admin') {
return $next($request);
}

if ($request->getPathInfo() === '/quickbooks/connect') {
return $next($request);
}

Location::addGlobalScope(
fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()),
);


if (Filament::getTenant()->id !== Auth::user()->latest_team_id) {
Auth::user()->latest_team_id = Filament::getTenant()->id;
Auth::user()->save();
}

return $next($request);
}
}
Want results from more Discord servers?
Add your server