Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jj63systems's avatar

Jetstream / how set selected team for session?

Hi all, using Jetstream (with Filament if that makes a difference) - I want a user to be presented with a "team selector" after login if they have access to more than 1 team.

This works - but I am having trouble to understand what variables I will set within my session when the user selects a team to work under.

Can someone identify what I should be setting? I know there is a current_team_id on my users table but setting that in isolation doesn't seem to do the trick.

I'm not sure if it's an important detail but I am using stephenjude/filament-jetstream

Appreciate any pointers - thanks, j

0 likes
3 replies
LaryAI's avatar

Certainly! In Laravel Jetstream, the current team for a user is managed by the current_team_id column on the users table. However, simply updating this column in the database is not enough to update the "active" team in the current session. Jetstream uses the currentTeam relationship on the User model, and it also caches the current team in the session for the duration of the request.

How Jetstream Handles Current Team

When a user switches teams, Jetstream typically does the following:

  1. Updates the current_team_id on the user model.
  2. Updates the session so that currentTeam reflects the new team.

Jetstream provides a method for this:

auth()->user()->switchTeam($team);

This method will:

  • Update the current_team_id on the user.
  • Update the session so the user's current team is correct for the request.

Example: Setting the Current Team

Suppose you have a team selector in your UI. When the user selects a team, you can handle the switch like this in your controller:

public function switchTeam(Request $request)
{
    $team = Team::findOrFail($request->team_id);

    // Make sure the user belongs to this team
    if (! auth()->user()->belongsToTeam($team)) {
        abort(403);
    }

    // Switch the team using Jetstream's method
    auth()->user()->switchTeam($team);

    return redirect()->back();
}

What Happens Under the Hood

  • The switchTeam method updates the current_team_id in the database.
  • It also updates the session so that auth()->user()->currentTeam returns the correct team for the rest of the session.

With Filament-Jetstream

If you're using stephenjude/filament-jetstream, the logic is the same, since it extends Jetstream's team features. You can use the same switchTeam method.

Summary

You should not manually set session variables.
Just use:

auth()->user()->switchTeam($team);

This will handle both the database and session updates for you.


References:

Let me know if you need a full example with routes or Filament-specific integration!

jj63systems's avatar

@LaryAI Thanks for the suggestion - I'm stuck - my return statement appears to do nothing - no error raised but also the user doesn't get taken anywhere. Not clear in fact what the intended user experience of the suggested code is?

This is my function

    public function handleTeamSelection(?int $teamId = null): \Illuminate\Http\RedirectResponse
    {


        $user = Auth::user();
        $team = $user->allTeams()->where('id', $teamId)->first();


        // Switch the team using Jetstream's method
        auth()->user()->switchTeam($team);

        return redirect()->back();

}

This is invoked from blade code (the wire:click):


<x-filament::page>
    <div class="space-y-6">
        <h2 class="text-lg font-semibold">Select a Team</h2>

        <div class="mt-4 space-y-4">
            @foreach (Auth::user()->allTeams() as $team)
                <button
                    wire:click="handleTeamSelection({{ $team->id }})"
                    class="w-full px-4 py-2 text-left bg-gray-100 hover:bg-gray-200 rounded-lg"
                >
                    {{ $team->name }}
                </button>
            @endforeach
        </div>
    </div>
</x-filament::page>

Appreciate suggestions. Ideally I just want to navigate to the dashboard - which I have coded before like this

 $this->redirect('/app');

But when I use that, the selected team information appears to be being reset to the value that was present before a new team was selected.

jj63systems's avatar

Continuing this - there must be something fundamental going wrong here.

My code now is

    public function handleTeamSelection(?int $teamId = null): void
    {


        $user = Auth::user();
        $team = $user->allTeams()->where('id', $teamId)->first();


        // Switch the team using Jetstream's method
        auth()->user()->switchTeam($team);
        die;

When I run this and immediately look into the database I can see that my team change selection has been recorded (users.current_team_id) - but as soon as I navigate to any other page, the current-team_id is reset to the "default" team_id (that defined within the team_user table.

Do I need to add a row to this table to enable the team change to remain persistent perhaps?

Please help :)

thx j

Please or to participate in this conversation.