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:
- Updates the
current_team_idon the user model. - Updates the session so that
currentTeamreflects the new team.
Jetstream provides a method for this:
auth()->user()->switchTeam($team);
This method will:
- Update the
current_team_idon 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
switchTeammethod updates thecurrent_team_idin the database. - It also updates the session so that
auth()->user()->currentTeamreturns 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!