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

gwa's avatar
Level 3

Switch Team after update

Hey I just updated to the new Spark version. And now Team Switching is not working any one has the same Problem?

I think the Problem is that in the TeamController.php the switchCurrentTeam -> CanJoinTeams.php switchToTeam function want a Team Object.

/**
     * Switch the current team the user is viewing.
     *
     * @param  Request  $request
     * @param  \Laravel\Spark\Team  $team
     * @return Response
     */
    public function switchCurrentTeam(Request $request, $team)
    {
        abort_unless($request->user()->onTeam($team), 404);

        $request->user()->switchToTeam($team);

        return back();
    }
/**
     * Switch the current team for the user.
     *
     * @param  \Laravel\Spark\Team  $team
     * @return void
     */
    public function switchToTeam($team)
    {
        if (! $this->onTeam($team)) {
            throw new InvalidArgumentException("The user does not belong to the given team.");
        }

        $this->current_team_id = $team->id;

        $this->save();
    }

but its called over a route with an id.

$router->get('/'.$pluralTeamString.'/{team}/switch', 'TeamController@switchCurrentTeam');
0 likes
3 replies
KevinKirchner's avatar

I realized that it's actually working for me. It appeared to not work because I was on the Team Profile page and expected it to show the current team's profile page when I switched teams, but you can view any team's profile regardless of what your current team is.

hs's avatar

I think it might make more sense to redirect to a different page if you're on the Team Profile page and trying to switch. To make this happen, I added the following 2 lines of code to /spark/src/Http/Controllers/TeamController.php at line 84 in the switchCurrentTeam function:

$route_down = extract_uri( $request->headers->get('referer'), 1);

return $route_down=='/settings/teams' ? redirect( $route_down . '/' . $team->id ) : back();

And this in my helpers.php:

if( ! function_exists('extract_uri'))
{
  function extract_uri($url, $remove_levels=0)
  {
    $parsed = parse_url($url);
    $uri = $parsed['path'];
    // strip levels off the URI
    if( $remove_levels )
    {
      $parts = explode('/', $uri);
      if( count($parts) > 1 )
      {
        array_pop( $parts ); // remove last item in array
        $uri = implode('/', $parts);
      }
    }
    return $uri;
  }
}
1 like

Please or to participate in this conversation.