Why a multi-tenant Laravel page suddenly looks empty: current workspace stored on the user row

From our own projects · Published 29 November 2026

One morning a templates page in our own Laravel app looked empty. The templates were not gone. Another session on the same login had switched the active workspace, and the page was showing a different workspace’s data. This page explains the mechanism, how to confirm it, and a design that avoids it.

What happened

Our app is multi-tenant. A user can belong to several workspaces, and every page shows data for the user’s current workspace. The page was empty because the user was looking at a workspace with no templates. The data was safe in the workspace they normally used. Switching back showed their templates again.

The cause was an automated QA agent that signs in with the same login as the person using the app. It switched to its own QA workspace to do its work. The switch changed what the person saw in their own browser.

Why a second session can change your view

The current workspace is stored as a column on the user. This is the code in our HasWorkspaces trait:

public function switchToWorkspace(Workspace $workspace): void
{
    if (! $this->onWorkspace($workspace)) {
        throw new InvalidArgumentException('User does not belong to this workspace');
    }

    $this->activeWorkspace = $workspace;

    $this->current_workspace_id = $workspace->id;
    $this->save();
}

current_workspace_id is saved on the users row. It belongs to the user, not to a browser session. On the next request, every session for that user reads the same column:

public function currentWorkspaceId(): ?int
{
    if ($this->activeWorkspace !== null) {
        return $this->activeWorkspace->id;
    }

    if ($this->current_workspace_id) {
        $workspace = Workspace::find($this->current_workspace_id);
        if ($workspace && $this->onWorkspace($workspace)) {
            $this->switchToWorkspace($workspace);
            return $this->activeWorkspace->id;
        }
    }
    // ...falls back to the user's first workspace
}

So whichever session switched last decides what all sessions show. Two browser tabs on different workspaces will also fight over this, not only automated agents.

How to confirm it

  1. Note the current_workspace_id for the user in the database.
  2. In another session, switch workspace.
  3. Reload the first session. If it now shows the other workspace’s data, the setting is shared.

You can also look at the updated_at of the user row. If it changed when nobody edited the profile, something switched the workspace.

A safer design

Keep the current workspace per session, not per user.

  • Store the active workspace id in the session (session('workspace_id')) and validate on each request that the user belongs to it.
  • Put it in the URL when you can (/w/{workspace}/templates), so the page itself says which workspace it is showing. Two tabs then work independently.
  • Use the user column only as the default for a new session, “the workspace this user last chose”, and never read it again after the session has its own value.
  • Always show the active workspace name in the header, so an empty page is easy to explain.

If you keep the column, scope data access explicitly. Do not read the active workspace from a global. Pass the workspace into queries and authorise against it.

For automated QA

Give automated agents their own user account and their own workspace membership, not a copy of a person’s login. If an agent must use a shared login, have it work in its own session and never write to the user row. Otherwise it will change what the real person sees, and a data-looks-missing report will cost an hour of checking that nothing was lost.

Our network